In this short post, I assume you have basic knowledge of working with the WP_Query class in WordPress. Before we begin, let’s provide some basic examples similar to those found in the WordPress Codex.
These examples are relevant when you want to create a filter for posts or an advanced search in WordPress or in a WooCommerce store.
As you probably know, you can add meta fields (metadata) to all posts under the ” Custom Fields ” box.
For example, if you want to retrieve all posts with a meta key named show_on_homepage and a meta value equal to on, you can do this as follows:
$sv_args = array(
'meta_key' => 'show_on_homepage',
'meta_value' => 'on'
);
$sv_query = new WP_Query( $sv_args );
On the other hand, if you want to retrieve all posts except those with the meta key and value mentioned above, you can do this as follows with the following parameters:
$sv_args = array(
'meta_key' => 'show_on_homepage',
'meta_value' => 'on',
'meta_compare' => '!='
);
$sv_query = new WP_Query( $sv_args );
Note that the examples in this post are simplified and lack some WP_Query parameters such as
posts_per_page
orpost_type
.
These are very basic examples. This post is mainly about the meta_query
parameter, which allows you to create really cool filters for your posts or products.
Examples of using meta_query
Retrieving posts with a specific value in the custom field
A simple example, let’s retrieve all posts with a custom field named color and a value of white:
// the meta_key 'color' with the meta_value 'white'
$sv_args = array(
'meta_query' => array(
array(
'key' => 'color',
'value' => 'white'
)
)
);
$sv_query = new WP_Query( $sv_args );
If you go to the WordPress admin interface and look at the edit page of each of those posts that are returned in this loop, you will find the following custom field:
![Custom Fields Metabox](https://savvy.co.il/wp-content/uploads/2018/03/custom-fields-metabox.jpg)
To retrieve all posts but not those with the custom field color with the value white:
$sv_args = array(
'meta_query' => array(
array(
'key' => 'color',
'value' => 'white',
'compare' => '!='
)
)
);
$sv_query = new WP_Query( $sv_args );
Now let’s retrieve all posts with the value white or green:
// custom field name is color and custom field value is 'white' OR 'green'
$sv_args = array(
'meta_query' => array(
array(
'key' => 'color',
'value' => array('white','green'),
'compare' => 'IN'
)
)
);
$sv_query = new WP_Query( $sv_args );
Retrieve all posts (products in a WooCommerce store, for example) except for white or green products:
$sv_args = array(
'meta_query' => array(
array(
'key' => 'color',
'value' => array('white','green'),
'compare' => 'NOT IN'
)
)
);
$sv_query = new WP_Query( $sv_args );
Retrieving all fields within a specific numeric range
For example, let’s filter in a WooCommerce store all products priced between 2000 and 4000:
// the product price is more than 2000 and less than 4000
$sv_args = array(
'meta_query' => array(
array(
'key' => 'price',
'value' => array( 2000, 4000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$sv_query = new WP_Query( $sv_args );
Numeric comparison
In the next example, let’s filter all products priced at 2000 and above:
$sv_args = array(
'meta_query' => array(
array(
'key' => 'price',
'value' => 2000,
'type' => 'numeric',
'compare' => '>='
)
)
);
$sv_query = new WP_Query( $sv_args );
Products priced below 4000:
$sv_args = array(
'meta_query' => array(
array(
'key' => 'price',
'value' => 4000,
'type' => 'numeric',
'compare' => '<'
)
)
);
$sv_query = new WP_Query( $sv_args );
Retrieving posts by multiple custom fields
Let’s combine several of the examples we presented earlier:
// the 'color' is 'white' AND the 'price' is more than 2000 and less than 4000
$sv_args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'show_on_homepage',
'value' => 'on'
),
array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'white'
),
array(
'key' => 'price',
'value' => array( 2000, 4000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
)
);
$sv_query = new WP_Query( $sv_args );