21st Jul '24
/
0 comments

Bricks Query Loop – Posts Authored by Logged in User

Looking to limit the posts in a Bricks query loop to those authored by the currently logged-in user?

Click the query icon for the query loop, enable PHP editor and paste:

if ( is_user_logged_in() ) {
  return [
    'post_type' => 'post', // change this to your post type
    'posts_per_page' => 100, // a large number
    'no_found_rows' => true,
    'author' => get_current_user_id(),
  ];
} else {
  return [
    'post__in' => [ 0 ],
  ];
}

Click the Sign code button.

You may want to apply a query_results_count dynamic data condition on the containing parent Section of this query loop-enabled Block to ensure that the entire section is output only if there is at least 1 post.

where mjomxr is the Bricks ID of the query loop-enabled element.

This means the Section won’t be rendered for users who are not logged in and for users who have not published (authored) any posts of the specified post type.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Post Data for the Current User in Bricks with Meta Box

Post Data for the Current User in Bricks with Meta Box

How to configure a query loop to output the post data of the SalesRep CPT associated with the current user.
Categories:
Pro
Dynamic Horizontal Posts Accordion in Bricks

Dynamic Horizontal Posts Accordion in Bricks

Update on 8 Sep 2024: You may want to follow the newer version that considers accessibility. Looking to make a Horizontal Image Accordion dynamic? This…
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
Pro
Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Updated on 12 Dec 2023 Bricks Query Loop popup does not have a control for setting the number of posts to be output when the…
Pro
Accordion Grid Query Loop in Bricks

Accordion Grid Query Loop in Bricks

How to set up a accordion in Bricks such that the "titles" are arranged in a grid with the full width content opening below.
Categories: