17th Nov '25
/
5 comments

Latest Featured Posts First in Bricks Query Loop (No Duplicates)

In the Bricks Facebook group a user asks:

Brain has frozen – need some help please. [ query help… not grid display help ]

I would like to display Bricks builder query results from 5 CPTs (resources). I want the first 2 results of the query to be the newest 2 posts marked as ‘Featured’ (based on a yes/no ACF field) from the different CPTs.

I would like the rest of the results to continue in date order and not repeat the 2 featured posts.

There will always be at least 2 featured posts, but only the two latest should be featured first. The 3rd, 4th, 5th etc featured post should just appear in the normal query loop output.

And the results use a Bricks filter… so having 2 query loops is not an option.

We can use AI to help unfreeze our brains sometimes or at least have it write the code with us after giving directions on the logic 😅.

Here’s the PHP query for this:

// Define your post types - adjust these to match your actual CPT slugs
$post_types = ['resource']; // or ['type1', 'type2', 'type3', 'type4', 'type5']

// Step 1: Get the 2 latest featured posts
$featured_args = [
	'post_type' => $post_types,
	'posts_per_page' => 2,
	'orderby' => 'date',
	'order' => 'DESC',
	'meta_query' => [
		[
			'key' => 'featured', // Replace with your ACF field name
			'value' => '1',
			'compare' => '='
		]
	],
	'fields' => 'ids',
	'no_found_rows' => true,
];

$featured_post_ids = get_posts( $featured_args );

// Step 2: Get all other posts (excluding the 2 featured posts)
$other_args = [
	'post_type' => $post_types,
	'posts_per_page' => 100, // Adjust based on total posts expected
	'orderby' => 'date',
	'order' => 'DESC',
	'fields' => 'ids',
	'no_found_rows' => true,
];

// Exclude the 2 featured posts if found
if ( ! empty( $featured_post_ids ) ) {
	$other_args['post__not_in'] = $featured_post_ids;
}

$other_post_ids = get_posts( $other_args );

// Step 3: Merge arrays - featured posts first, then the rest
$final_post_ids = array_merge( $featured_post_ids, $other_post_ids );

// Return empty result if no posts found
if ( empty( $final_post_ids ) ) {
	return ['post__in' => [0]];
}

// Step 4: Return query args maintaining the order
return [
	'post_type' => $post_types,
	'posts_per_page' => -1, // Show all, or set a number for pagination
	'post__in' => $final_post_ids,
	'orderby' => 'post__in', // Maintains the order we set
	'no_found_rows' => true,
];
Get access to all 610 Bricks code tutorials with BricksLabs Pro

5 comments

  • Thanks for sharing, but this doesn' t work for me. The featured posts are not the first, it just simply put everything in date order. I have almost the same task, with a slight difference. I need 4 featured post from the same acf post type, but with random order on the first 4, and in random order the rest afterwards and it needs to be filterable. I just tried this too, because it's almost the same, but I don't think it works as it should. The AI ​​told me, if I use a meta query, I can't specify an order. I don't kno about that...

  • Thanks for sharing Sridhar.

Leave a Reply to Rene (Cancel Reply)

 

Related Tutorials..

Random Posts in the Same Category as the Current Single CPT Post in Bricks

Random Posts in the Same Category as the Current Single CPT Post in Bricks

Here’s how query loop can be set in Bricks to show related posts by a taxonomy’s terms
Categories:
Pro
Hierarchical Posts with Nested Query Loops in Bricks

Hierarchical Posts with Nested Query Loops in Bricks

Learn how to set up nested query loops in Bricks to show hierarchical posts with child posts grouped under their corresponding parent posts
Categories:
Pro
WordPress Posts using WP REST API in Bricks

WordPress Posts using WP REST API in Bricks

The steps to display posts from any WordPress site that has WP REST API enabled in Bricks builder using a custom query type.
Categories:
Pro
Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

Nested Meta Box Query Loop Inside a CPT Query Loop in Bricks

This Pro tutorial provides the steps for setting up a cloneable Meta Box group query inside a CPT query in Bricks. Scenario: CPT: course Meta…
Categories:
Pro
Previous Two Posts in Bricks Query Loop

Previous Two Posts in Bricks Query Loop

This Pro tutorial shows how we can modify the parameters of a Bricks query to limit the posts to the previous two whilst cycling to…
Load more for Query loops in Bricks

Load more for Query loops in Bricks

Did you know that Bricks has AJAX loading for query loop posts on-click out of the box?
Categories:
Pro
CMB2 Repeatable Group Bricks Query Loop

CMB2 Repeatable Group Bricks Query Loop

This Pro tutorial provides the steps to create a custom query loop type for outputting the subfield values of a Group field (repeatable) when using…
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:
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: