If you design or develop WordPress themes or plugins, there’s a good chance that someday you’ll need to make a query for custom meta fields. These are those completely custom key/value pairs that you can attach to any post, page, or custom post type. WordPress has a basic UI for them by default, or you can use something likeAdvanced Custom Fields to get fancy with them. But under the hood ACF uses regular ol’ custom fields.
This very snippet page you are looking at right now was written in 1999. At that time, in order to query for posts with particular custom fields, you would need to use the`$wpdb` global variable. That can be used for creating MySQL queries that the WordPress WP_Query() class doesn’t support. Fortunately today, WordPress does have arguments that support queries for custom meta fields.
Here, we’ll cover the different ways you can request and loop over posts with particular custom fields (and their values). You’ll be able to use this information whether you use the WP_Query
class, query_posts()
, or get_posts()
. Since query_posts()
andget_posts()
are wrappers for the WP_Query
class. They all accept the same arguments.
The Query Arguments
Here is a basic example of a WordPress query taken from the WordPress Codex.
<?php // The Query $the_query = new WP_Query( $args ); // The Loop if ( $the_query->have_posts() ) { echo '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); echo '<li>' . get_the_title() . '</li>'; } echo '</ul>'; } else { // no posts found } /* Restore original Post Data */ wp_reset_postdata();
The $args
is the important bit there. We’ll be passing different arguments to make this work how we want.
When querying for custom meta, there are two “groups” of arguments that you can use. One group is for a simple custom meta field query and the other group of for more complex custom meta fields queries. Let’s start with the simple group.
meta_key
The meta_key
argument will query any post that has the custom field meta ID saved to the database, whether or not there is a value saved for the field. The meta_key
is the ID that you give to your meta fields. Like this:
This example will query any post that has the custom meta field with the ID of “field1”.
$args = array( 'meta_key' => 'field1' );
meta_value
The meta_value
argument queries post that have the value you define. Themeta_value
argument is used for string values. This example will query any posts with a custom meta field that has the value “data1”.
$args = array( 'meta_value' => 'data1' );
You can also combine the two. This example will only query posts that have the custom meta field with the ID of “field1” that has the value of “data1”.
$args = array( 'meta_key' => 'field1', 'meta_value' => 'data1' );
meta_value_num
The meta_value_num argument is similar to the `meta_value` argument. Where themeta_value
argument is ment for string values the meta_value_num
is meant fornumerical values.
This example shows how to query the “field1” custom meta field if it has a value of “10”.
$args = array( 'meta_key' => 'field1', 'meta_value_num' => '10', );
meta_compare
The meta_compare
argument does exactly what it sounds like. It’ll allow you to use comparators with the `meta_value` and `meta_value_num` arguments. The comparators you can use are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘NOT EXISTS’, ‘REGEXP’, ‘NOT REGEXP’ or ‘RLIKE’.
Here’s an example that shows how to query any posts that don’t have the value of “data1”.
$args = array( 'meta_key' => 'field1', 'meta_value' => 'data1', 'meta_compare => '!=', );
Leave a Reply