Get 30% discount on lifetime membership by using BFCM2023 coupon.
Offer ends on 30th Nov 2023 at 11:59:59 PM UTC.

Multi-choice Select Field Based Querying in Bricks

One of our site members asked:

Any chance you guys could create a tutorial for to show us how to be more granular with meta queries?

For example, I have a grid of CPTs queried.

I want to query CPTs that match Manufacturer: 1 or 2 AND Material: stainless steel

Right now I can only do Manufacturer 1 AND 2 AND Stainless Steel (which would only be CPTs tagged as all 3) or Manufacturer 1 OR 2 OR Stainless Steel which would be CPTs that have any of one those.

For example, any way to say Manufacturer: 1, 2 AND Material: Stainless Steel?

Let's take a scenario where a product post type has a corresponding manufacturer and material custom fields.

Product 1
Manufacturer: Manufacturer 2
Materials: Copper, Iron

Product 2
Manufacturer: Manufacturer 1
Materials: Stainless steel

Product 3
Manufacturer: Manufacturer 3
Materials: Iron, Stainless steel

Product 4
Manufacturer: Manufacturer 2
Materials: Stainless steel

Requirement: Query CPT items that match Manufacturer: 1 or 2 AND Material: Stainless steel
i.e., Product 2 and Product 4.

Custom field group:

Note that "Select multiple values" is on for both the fields.

This Pro tutorial provides the steps for adding a custom meta_query that will filter or limit the posts to match the following:

manufacturer (array) is Manufacturer 1 OR Manufacturer 2
AND
material (array) is Stainless Steel

in Bricks.

Step 1

Edit the Page/template in which you would like to show the CPT items with Bricks.

Inside a Section's Container, add a Posts element or set up Query Loop on a Container/Block (I typically use a Block element for query loops).

Edit the Query settings.

Post type: Products (change this to your post type, if different)

Posts per page: 100 (a large enough number that the total number of items of this CPT will never exceed in your site)

Do not set a meta query here because it is not possible to do what we want via the query popup controls.

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

Instant access to all 250+ Bricks code tutorials with BricksLabs Pro