Posts from Random Categories in Bricks

Updated on 31 Jul 2023

In BricksLabs Facebook group a user asked:

How would you query 3 WordPress posts from 3 different categories with Brick Builder query loop function? Case #1: the categories do not matter. Case #2: the categories matter.

This Pro tutorial provides the steps to tackle case #1 wherein we construct an array of IDs of latest posts assigned to 3 random categories in WordPress and use Bricks builder's bricks/posts/query_vars filter to pre-filter or limit the posts output by a Bricks query loop.

Step 1

Set up a query loop and set the query type to Posts.

Do not set any other settings.

Step 2

Add the following in child theme's functions.php or a code snippets plugin:

This is a BricksLabs Pro tutorial.

For full access login below or create your BricksLabs Pro account

Get access to all 525 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

How to List Your Posts Divided by Categories in Bricks

How to List Your Posts Divided by Categories in Bricks

In this tutorial, we'll learn how to display a list of posts divided by each category. The DOM tree Here is how I structured my…
Categories:
Pro
“My Favorites” in Bricks

“My Favorites” in Bricks

This Pro tutorial provides the steps to set up and use My Favorites in Bricks Builder.
Categories:
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:
How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

How to populate a map with dynamic markers from a CPT using ACF in Bricks (PART 2)

This tutorial provides the PHP & JS codes that can be pasted in order to create a flying effect on map markers each time your…
Loop Item ID and Loop Item Object in Bricks

Loop Item ID and Loop Item Object in Bricks

If you are working on a Bricks site with specific requirements, chances are you need to grab the ID and/or object of the current item…
Categories:
Pro
Posts Grouped by Post Dates in Bricks

Posts Grouped by Post Dates in Bricks

Setting up query loops for “Post Dates” with an the inner loop displaying all posts published on that day.
Categories:
Pro
Non-empty events sorted by event date and other posts below in Bricks

Non-empty events sorted by event date and other posts below in Bricks

Updated on 15 Jan 2024 Consider a scenario where you are showing posts from multiple post types namely post, event and game in a single…
Categories: