Introduction to ACF Meta Queries

This guide will hopefully help you if you’re entirely new to the concept of “meta queries” and how you might use them in ACF Engine. In WordPress post types are used to store different types of content, or in another terminology, different types of database records. By default this is the “post” for blog posts, and a slightly different type, “pages” which are used to build a WP site’s pages. Now when you have different post types (ACF Engine can be used to create custom post types), usually it’s because you have some different data to store in fields. This is where ACF comes in, as the name implies (Advanced Custom Fields) it’s used to create the interface for fields. And ACF by far has the best selection, and most robust functionality of any plugin used for this purpose. Which is why professional WP developers use ACF so often.

When you make an ACF field, no matter what the type is, you are in WordPress lingo create a “meta field”. That’s basically what ACF is, though ACF is known in large part for it’s interface that allow rapid creation of fields. Behind the scenes, it uses a fairly common approach to data storage, by storing field data as post meta. So let’s imagine you have a custom post type named “vehicle”, and you add a field named “doors” to store how many doors the vehicle has. This could be a number field, it could be a choice field with options like 2D (two doors) or 4D (four door). Note that with a choice field, you might decide to provide the options like this, 2 : 2D and 4 : 4D, this way only the number is stored, which may make querying easier later. Just remember, generally, numeric data is easier to match and sort.

Using our example of the vehicle custom post type, when you add a vehicle post and you select the 4D (four door) option, this data for field “door” is stored as post meta. This means it’s associated to the post, but it is also separate in the database. ACF Engine Queries are going to enable you to do a meta query to fetch vehicles using all the attributes you store as ACF fields.

For instance, let’s say you only want to show the user four door vehicles. First you’ll create a new Query in ACF Engine. Just head to the menu for ACF Engine, click on Queries, and select “Add New”. Like any other ACF Engine post, you’ll find a set of fields to fill in to define your Query. You’ll need to enter the post type key which in this case is “vehicle”. Then for this example you can leave the other settings, but just realize that if you wanted to limit the number of results you could do that, or adjust other settings to alter the results from your Query.

In the Meta Queries section you can add as many different queries as you need. There is an option to determine if these queries should be combined, or work as “or queries” meaning only 1 has to match to pull a record from the database.

Leave a Reply