We are increasing the price on 14th may 2024. 

Premium add-on for Bricks Builder

Displaying current user posts with Query Loop

How do you show the current user’s posts with Query Loop builder? In this tutorial, I shall show you how you can get it in the Bricks Builder.

Step 1: Make your query loop from the bricks editor like this (you can adjust the arguments as per your requirement)

Step 2: Now open the functions.php file of your bricks’ child theme and drop this PHP code.

add_filter( 'bricks/posts/query_vars', 'bu_get_current_user_posts', 10, 3 );
function bu_get_current_user_posts( $query_vars, $settings, $element_id ) {
	if( $element_id == 'pilbnn' && is_user_logged_in() ) {
		$query_vars['author'] = wp_get_current_user()->user_id;
	}
	
	return $query_vars;
}

Bricks Query Loop builder has a filter bricks/posts/query_vars. With this filter, you can modify the default query args and manually add the new args via PHP code. This filter has 3 params:

$query_vars : all query arguments are saved in this variable
$settings: this variable is holding the element’s settings
$element_id: It is holding the element ID. So you can target this variable and apply your custom code for that particular element.

Explaining the above code:

I used the IF statement. Arguments will update if a user is logged into the site. is_user_logged_in() WP function is checking it.

I do not want that the above filter is applied to the entire site. So I used another condition $element_id == ‘pilbnn’. My container ID is pilbnn. You will change it with your query loop builder container ID.

Element ID
Element ID

wp_get_current_user() WP function is returning the current user object who is currently logged into your site. I am getting the user ID with wp_get_current_user()->user_id and passing into the “author” query vars.

Fills In: 

3 comments

  • roundedhexagon

    Hi guys,

    This doesn’t seems to work for CPT posts. I set up everything correctly: changed the “$element_id ” to the correct container ID, but still shows all the CPT posts not only the post of the current author.

    Thanks!

    • If you need make it work with a CPT, you need to add another line of code, something like:

      $query_vars[‘post_type’] = ‘otherposttype’;

      This to specify to bricks you need this too.

    • A

      Can you share the wp-admin access once? I shall check it once.

Leave your comment

Search Tutorials

Request TutorialsFeatures Request