Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Query loop current post author

  • Dear support team,

    The “Current post author” option under the Query loop doesn’t seem to work as I thought. Perhaps there’s a bug or I’m not using it as designed. Hopefully, you can shed some light.

    Desired result:
    Return only the posts (ACF custom post type) that have the currently logged user as the author.

    Background:
    – The author has a custom role, instead of preset WP user roles.

    Attempts to resolve:
    – Remove the filter > query loop worked normally
    – Added the filter > loop returned nothing
    – Switch the user’s role from a custom role to “Author” (with filter) > loop still returned nothing

  • The current post author means the author of the page which has the query loop, so if the page’s author is you, then the query loop will return all posts that are published by you.

    To achieve what you want, it will require custom code to alter the query loop.

    Try this:

    1. Add this PHP code, and change the user role from administrator to your custom user role.

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
        // Check if the class name contains 'current-user-posts' and not in admin area
        if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'current-user-posts' ) !== false ) {
            // Check if the user is logged in
            if ( is_user_logged_in() ) {
                $current_user = wp_get_current_user();
    
                // Check if the user is an administrator
                if ( in_array( 'administrator', (array) $current_user->roles ) ) {
                    $user_id = $current_user->ID;
    
                    // Modify the query arguments to show all posts
                    return array_merge( $query_args, array(
                        'author' => $user_id,
                    ) );
                }
            }
        }
    
        // If conditions are not met, return the unmodified query arguments
        return $query_args;
    }, 10, 2 );
    

    2. Add this class current-user-posts to the grid block of the query loop.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    3. Remove all parameters from the query loop, only keep the post type and posts per page.

  • Hi Ying, this is awesome, thank you so much!!

  • You are welcome   🙂

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.