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

Leave the first comment

 

Related Tutorials..

Pro
Post ID Query Variable-based Post Output in Bricks

Post ID Query Variable-based Post Output in Bricks

How to query a single post of a custom post type using the post ID passed to a page via a URL parameter.
Pro
Term Parent Slug Data Attribute in Bricks

Term Parent Slug Data Attribute in Bricks

How we can set a data attributes on a child taxonomy dynamically based on the parent taxonomy.
Categories:
How to create filters with IsotopeJS in Bricks (Part 3): apply multiple filters

How to create filters with IsotopeJS in Bricks (Part 3): apply multiple filters

This tutorial will review how to apply multiple filters to an isotope container using the IsotopeJS library‘s features in Bricks.
Pro
Dynamic Remaining Posts Count for Load more Button in Bricks

Dynamic Remaining Posts Count for Load more Button in Bricks

A user asked in the Bricks Facebook group: I have a loop of a CPT, showing 10 in a total of 16 posts and a…
Pro
How to Insert an Ad or an Element Between Posts in Bricks

How to Insert an Ad or an Element Between Posts in Bricks

How to insert an image or any Bricks element between posts output by a query loop.
Categories:
Tags:
Pro
ACF User Field Bricks PHP Query

ACF User Field Bricks PHP Query

In the Bricks Facebook group a user asks: my end goal was to create a set of cards that would display the Users that are…
Categories:
Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

In the past we showed how a Bricks posts query loop can be filtered by Meta Box group's subfield of types text/number and Datepicker here.…
Categories: