The meta_query
is a parameter in WordPress that is used to filter posts or custom post types based on the values of their custom fields. It is a sub-parameter of the WP_Query
class and is used to specify one or more conditions that must be met by the custom fields of the posts to be retrieved.
The meta_query
parameter accepts an array of arrays, each of which represents a single condition. Each condition array can contain the following keys:
key
: the name of the custom field to be queriedvalue
: the value of the custom field to be matchedcompare
: the comparison operator to be used (e.g.=
,!=
,>
,<
,LIKE
, etc.)type
: the data type of the custom field (e.g.NUMERIC
,BINARY
,CHAR
,DATE
, etc.)
Here’s an example of how to use the meta_query
parameter to retrieve posts that have a custom field called “price” with a value greater than or equal to 100:
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'price',
'value' => 100,
'compare' => '>=',
'type' => 'NUMERIC'
)
)
);
$query = new WP_Query( $args );
Note that the meta_query
parameter can be used in combination with other parameters, such as post_type, category, tag, etc., to refine the query results further.