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..

How to apply a custom query var to multiple query loops in Bricks

How to apply a custom query var to multiple query loops in Bricks

In this tutorial, we'll learn how to apply a PHP filter to modify the query var of multiple query loops in a single function Introduction…
Categories:
Pro
Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Displaying Gallery of SureCart Product Images with Bricks’ Query Loop

Custom query loop for looping through SureCart product images.
Categories:
Tags:
Pro
Dynamic Posts Lightbox in Bricks using GLightbox

Dynamic Posts Lightbox in Bricks using GLightbox

Steps to set create a dynamic lightbox with content from the post inside of the query loop.
Categories:
Pro
Bricks Query Loop – How to Insert Elements After Every Nth Post

Bricks Query Loop – How to Insert Elements After Every Nth Post

Update: Follow this tutorial instead. This Pro tutorial provides the steps to output element(s) in a Bricks' query loop after every nth post. Use case:…
Categories:
Pro
Post Views Counter Query Loop in Bricks

Post Views Counter Query Loop in Bricks

How we can output the most viewed posts in your Bricks site in a query loop.
Categories:
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
Pro
6 Random Featured Posts in Alphabetical Order in Bricks

6 Random Featured Posts in Alphabetical Order in Bricks

How to output x number of random posts with a specific meta value set, in Alphabetical order.
Categories: