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 – only posts where the author is the currently logged in user

  • Hi,

    is there a way I can use Query Loop on a specific page to show posts for a given post type e.g. “Project” and only those where the currently logged in user is the author?

    I’ve tried Query Loop > Select post type > Project > Authors > Current post author (but I’m still getting all posts from all authors, so that’s probably not the right way to do it…)

    Thank you for your help πŸ™‚

  • Hi there,

    There’s no such built-in option in the query loop block, so please remove the author parameter from the query loop block first.

    However, you can try this method:

    1. Add an additional CSS class to the Grid block of the query loop block, eg, my-articles.
    https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    2. Add this PHP code:

    add_filter('generateblocks_query_loop_args', function ($query_args, $attributes) {
        if (
            !empty($attributes['className']) &&
            strpos($attributes['className'], 'my-articles') !== false &&
            is_user_logged_in()
        ) {
            $current_user = wp_get_current_user();
            $query_args['author'] = $current_user->ID;
        }
    
        return $query_args;
    }, 10, 2);

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    Let me know if this helps!

  • Hi, Ying,

    it works like a charm, you’re the queen of customer support,

    thank you πŸ™‚

  • Haha I’m the only female in GP’s tech support team, I guess that made me the queen lol πŸ˜›

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