29th Apr '23
/
0 comments

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 1Manufacturer: Manufacturer 2Materials: Copper, Iron

Product 2Manufacturer: Manufacturer 1Materials: Stainless steel

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

Product 4Manufacturer: Manufacturer 2Materials: Stainless steel

Requirement: Query CPT items that match Manufacturer: 1 or 2 AND Material: Stainless steeli.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 2ANDmaterial (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

Get access to all 621 Bricks code tutorials with BricksLabs Pro

Related Tutorials..

Pro
Posts Grouped by Authors in Bricks

Posts Grouped by Authors in Bricks

This Pro tutorial for Bricks users provides the steps to loop through users and inside that, through posts whilst setting the inner query's author parameter…
How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories:
Pro
Posts Published in the Last x Days in Bricks

Posts Published in the Last x Days in Bricks

Updated on 1 Feb 2024 This Pro tutorial shows how to limit the posts output by either the Posts element or a Query Loop in…