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 627 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Ordering Posts by Terms in Bricks

Ordering Posts by Terms in Bricks

How to output upcoming events at the top and past events below in a single query loop in Bricks.
Categories:
Pro
Parent post Bricks query loop

Parent post Bricks query loop

Updated on 5 Feb 2024 In the BricksLabs Facebook group a user asks: Is there a way to output the data of a parent post…
Categories:
Posts Related by Current Post’s Terms in Bricks

Posts Related by Current Post’s Terms in Bricks

Showing other posts assigned to the same categories/tags/custom taxonomy terms as the current post.
Categories:
Using WP Grid Builder Facets with Bricks’ Query Loop

Using WP Grid Builder Facets with Bricks’ Query Loop

Update: This tutorial was written before WPGB had a Bricks add-on. Now, it is advisable to use that add-on instead. Get it from here. Updated…
Categories:
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories: