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

Leave the first comment

 

Related Tutorials..

Pro
Nested Queries in Bricks – Posts Grouped by Published Years and Categories

Nested Queries in Bricks – Posts Grouped by Published Years and Categories

In the past we showed how posts could be grouped by years in Bricks. This Pro tutorial takes it further by grouping posts by categories…
Categories:
Pro
Upcoming Events Grouped by ‘Month Year’ in Bricks

Upcoming Events Grouped by ‘Month Year’ in Bricks

A custom query loop type for showing future events based on a date custom field using three nested query loops.
Displaying SureCart Featured Products & Products Collections with a Query Loop

Displaying SureCart Featured Products & Products Collections with a Query Loop

We walk through how to use a query loop to display SureCart products, featured products & collections.
Categories:
Pro
Custom SQL Ordering for Sorting Events in Bricks

Custom SQL Ordering for Sorting Events in Bricks

Showing events ordered by the event date with upcoming events in ascending order and past events in descending order.
Categories:
Pro
Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

Posts Grouped by Taxonomy Terms in Bricks using BricksExtras’ Dynamic Table and Pro Tabs

How to output any post type, with the taxonomy terms as Tab titles and posts inside tables with sortable columns.