One of the most powerful tools in WordPress is the ability to add custom fields to posts. Many developers use Advanced Custom Fields or custom meta fields for posts.
In this post we’ll be going over how to query posts by custom field using the meta_query function in WordPress. This is helpful for sites with a lot of custom functionality.
Query Posts By a Meta Value
Probably the most popular way to query posts with a custom field is targeting one specific meta value. For example let’s say we have a site where posts have a custom field used to determine if a post is “featured”. This is common when making a featured section on a WordPress site.
Of course the above code will vary depending on what you title your fields and meta keys but the ‘key’ is the name of the custom field while the ‘value’ is what we are querying posts for. The ‘compare’ field is used for a range of comparison operations we’ll go over later in the post.
Using this in WP_Query will allow us to get an array that is great for loop functions to display posts. In this post we’ll only be going over the meta_query functions rather than how to display WP_Query output.
Query Posts By Multiple Meta Values
If you want to check a meta key value against multiple values it’s best to use the ‘IN’ operator.
Query Posts By Multiple Meta Keys
Now that we’ve queried posts with a single meta key let’s do one with multiple fields/keys being queried. This will allow you to query posts based on multiple custom fields. This example combines both things we did above by nesting the queries into an array and adding a ‘relation’ to the meta_query.
The ‘relation’ field can be set to ‘AND’ or ‘OR’ so you can require all conditions to be met or just one.
Meta Query Comparison Operators
There are a few operators that will be helpful in querying your posts by meta query. Here is a quick breakdown of each comparison operator and how you can use them.
- ‘=’ Queries meta keys that equal a meta value
- ‘!=’ Queries meta keys that are NOT equal to a meta value
- ‘>’ Queries meta keys that are greater than the meta value
- ‘>=’ Queries meta keys that are greater than & equal to the meta value
- ‘<‘ Queries meta keys that are less than the meta value
- ‘<=’ Queries meta keys that are less than & equal to the meta value
- ‘LIKE’ Queries meta keys that contain a word/phrase (for example querying “red” would match the phrases “Red”, “looksred”, and “redstyle”)
- ‘NOT LIKE’ The opposite of above
- ‘IN’ Queries meta keys where the value exists in an array
- ‘NOT IN’ Queries meta keys where the value exists not in an array
- ‘BETWEEN’ Queries meta keys where the value is between two numbers
- ‘NOT BETWEEN’ Queries meta keys where the value is not between two numbers
- ‘EXISTS’ Queries meta keys where the value exists at all
- ‘NOT EXISTS’ Queries meta keys where the value doesn’t exist at all
- ‘REGEXP’ Queries meta keys based on a regular expression
- ‘NOT REGEXP’ Opposite of above REGEXP
How To Sort Posts By Meta Fields
Of course WordPress lets you sort posts using your custom fields using the ‘orderby’ option in WP_Query. You’ll need to set ‘orderby’ to be ‘meta_value’ and then set a ‘meta_key’ value to your field name. By default this is most useful when want your query to return alphabetically or sorted by number when your your field is a number.