Table of Contents:

TOC

The Bricks Builder Query Loop Meta Query Explained

The Bricks Builder query loop has a parameter to filter your query by adding a meta query. In this post, we will discuss what a meta query is in WordPress and how you can use the Bricks Builder query loop meta query to supercharge your query loops!

What Is A Meta Query In WordPress

A meta query in WordPress is a way to filter posts based on their associated metadata. Custom fields are metadata, so that means you can query a post or custom post type and then also reference your custom fields to further filter your query.

How Meta Queries Work

When you use a meta query, you will define the following parameters:

  • Meta Key: Specify the custom field (meta key) you want to search against.
  • Value: Determine the specific value that the custom field should match.
  • Comparison Operator: Choose how the custom field’s value should be compared to your provided value (e.g., =, !=, LIKE, IN, etc.).
  • Data Type (optional): Define the data type (such as NUMERIC, CHAR, etc.) to ensure the comparison is handled correctly.

What Is The Meta Key and How Do You Determine It

When working with custom fields for your meta query, the meta key will be your field ID in Meta Box or your field name in ACF. So for me it was “due_date”.

Here is what the field looks like in Meta Box:

Image showing Meta Box custom field settings for due_date field

Here is what it looks like to call the field inside Bricks query loop meta query:

Image showing bricks builder meta query parameters

What Is The Meta Value and What Does It Look Like

The easiest way to see the meta value in Bricks Builder is to add a text element, and return the custom field. By doing this, you can see the format of your field data.

bricks text element showing custom field data tag

Here is what it would look like on the front end. I can clearly see how the data is formatted in the database…yea this stuff matters when doing queries.

Image showing the custom field output on the front end

For simple text fields or number fields, the format is pretty easy to understand. But for dates and other data types, you may want to go ahead and try to see the exact format.

Meta Query Example

I was working on a project where I have a custom date field for all posts. This field was called “due date.” The field was used to store a date that some particular activity was to be completed by…you know, the due date.

I wanted to create a query loop that showed all posts where the due date was past due. This is a prime example for a meta query.

The first step is to create a query loop for your post type.

Image showing bricks builder query loop parameters

Next, you add the meta query parameters.

Image showing bricks builder meta query parameters

Lets Dive Deeper Into The Meta Query

There is a lot going on with this meta query. You have to make sure your meta key and the meta value are set up properly.

For my use case, I wanted to see posts where the custom due date field was less than or equal to the current date (WordPress date). That should show me all the posts that were past due.

The custom Meta Box Field

The format that Meta Box stores dates in the database looks like this:

2025-01-01

I wanted to compare that to the current_wp_date function and Bricks lets you format dates using dynamic data tags. This bit is important!

So I used the dyanmic data button in Bricks to call up the date like so:

2025-02-22

That would format the date exactly like Meta Box stores the date in the database.

The Compare Parameter

I wanted to see posts that were past due, so I used:

“Lesser or equal”

I could have used lesser, but lesser or equal got the job done by showing posts that were past due or technically due today.

The Date Type (Compare)

Bricks lets you pick the data type you are comparing. Since the two items I am comparing are dates, I chose the option:

“DATE”

No Need For Relation (AND / OR) In This Example

Since I only had one meta query, I did not need to set the relation parameter.

However, if you wanted to string together multiple meta queries, you could.

You could use the AND or OR operators to further filter down into your data.

If you had two custom fields for your posts, let use pets for an example, and you had breed and gender fields you could create a meta query using the AND relationship to call up Breed = “Lab” and Gender = “Male”.

You could also use the OR operator to return something like:

Breed = “Lab” OR Breed = “Shepherd”

Let’s Briefly Talk About Performance With Meta Queries

Complex meta queries involving multiple custom fields or complex comparisons can lead to slower query performance on larger databases.

This slowdown occurs because WordPress must scan through potentially large amounts of metadata stored in the wp_postmeta table, which may not be optimized for these types of queries by default.

To optimize your queries, consider adding indexes to frequently queried meta keys. Indexing these fields can significantly improve query speed by allowing the database engine to locate records more efficiently, rather than performing a full table scan.

Another effective strategy is to implement caching mechanisms for your query results.

Caching can reduce the load on your database by temporarily storing the results of a query and reusing them for subsequent requests, thus avoiding the need to run the same complex query repeatedly.

Frequently Asked Questions

Can I Use Group Fields With Meta Queries?

The short answer is no. It is not a best practice to store custom fields inside Meta Box groups when you want to call the field in a meta query. That’s because the group field ID or name is stored as the meta key in the database. The fields are then stored as a serialized array in the database and your custom field then has not meta key. This makes is near impossible to access it using the meta query builder in Bricks.

Can I combine meta queries with taxonomy queries?

Yes, you can combine meta queries with taxonomy queries. Bricks allows you to filter posts by taxonomies using the same general workflow as querying custom fields using the meta query builder.

What comparison operators can I use with meta queries?

Meta queries support several comparison operators, including =, !=, >, >=, <, <=, LIKE, IN, NOT IN, and BETWEEN. Do note that Bricks uses words like “lesser” and “lesser or equal” instead of the symbolto help users pick the right comparison operators.

Wrapping Up

When building complex query loops where you filter based on custom fields, the meta query parameters in the Bricks Builder query loop settings are VERY powerful.

To make sure your meta query works, you need to determine what the field (meta key) is called inside your WP database. You also need to see how the data is formatted in the database.

Once you know the field ID (meta key) and the meta value (the actual data in your custom field for a given post), you can build very complex queries using Bricks Builder.

I hope this tutorial helped you understand how to create meta queries using Bricks! If you have any questions, don’t hesitate to get in touch!

Need Help With Bricks Builder?

John the owner of Bricks Coach and the lead instructor.
Book a consultation

Related Posts

The Bricks Builder Query Loop Meta Query Explained

February 22, 2025

How To Send A Webhook To n8n With Header Authentication With Bricks Forge Pro Forms

February 8, 2025

How To Find The Form ID And Form Fields For A Bricks Builder Custom Form Action

January 5, 2025

How To Create An Age Gate With The Bricks Forge Node Editor

January 2, 2025

How To Clone A Github Repository To Your Local Drive

December 8, 2024

Review Your Cart
0
Add Coupon Code
Subtotal