WordPress WP_Query: Get Posts by Meta Values

As you know all the posts have the metadata you can fill in “Custom fields” metabox (it can also be hidden). So, for example, if you want to get a post with meta key show_in_slider and meta value true, you can do it in the following way:

<?php
$args = array(
	'meta_key' => 'show_in_slider',
	'meta_value' => 'true'
);
 
$allRecords = new WP_Query( $args );
?>

Otherwise, if you need to query all posts except the ones with this pair of meta key and value, you can use the following parameters:

<?php
$args = array(
	'meta_key' => 'show_in_slider',
	'meta_value' => 'true',
	'meta_compare' => '!='
);
 
$allRecords = new WP_Query( $args );
?>

Do not forget that all examples in this post are simplified, so, some of WP_Query parameters are missing, e.g posts_per_page or post_type.

This was very simple examples, just an introdution. But this post is about meta_query parameter. This parameter allows us to create a really cool post filters and search scripts.

meta_query Usage Examples

Get Posts with a Specific Custom Field Value

This is a simple example. Let’s get all the posts with custom field name “color” and custom field value “white”.

<?php 
// the meta_key 'blog_type' with the meta_value 'clasic'
$args = array(
	'meta_query' => array(
		array(
			'key' => 'blog_type',
			'value' => 'clasic'
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Get all the posts except the ones with meta key “blog_type” and meta value “clasic”:

<?php
$args = array(
	'meta_query' => array(
		array(
			'key' => 'blog_type',
			'value' => 'clasic',
			'compare' => '!='
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Now let’s get all the posts with clasic OR regular type custom field value:

<?php
// custom field name is blog_type and custom field value is 'clasic' OR 'regular'
$args = array(
	'meta_query' => array(
		array(
			'key' => 'blog_type',
			'value' => array('clasic','regular'),
			'compare' => 'IN'
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Get all the posts except clasic and regular posts:

<?php
$args = array(
	'meta_query' => array(
		array(
			'key' => 'blog_type',
			'value' => array('clasic','regular'),
			'compare' => 'NOT IN'
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Get Posts Within a Given Range of Numeric Meta Values

For example, let’s filter the post by hit’s rang:

<?php
// The post hit's is more than 9000 and less than 10000
$args = array(
	'meta_query' => array(
		array(
			'key' => 'hits',
			'value' => array( 9000, 10000 ),
			'type' => 'numeric',
			'compare' => 'BETWEEN'
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Numeric Comparison

The post hit’s in this example is 9999 or more than 9999:

<?php 
$args = array(
	'meta_query' => array(
		array(
			'key' => 'hits',
			'value' => 9999,
			'type' => 'numeric',
			'compare' => '>='
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

The post hit’s less than 100:

<?php
$args = array(
	'meta_query' => array(
		array(
			'key' => 'hits',
			'value' => 100,
			'type' => 'numeric',
			'compare' => '<'
		)
	)
);
 
$allRecords = new WP_Query( $args );
?>

Query Posts by Two Or More Custom Field Values

Let’s combine some of the previous examples:

<?php
// the 'blog_type' is 'clasic' AND the 'hits' is more than 9900 and less than 10000
$rd_args = array(
	'meta_query' => array(
		'relation' => 'AND',
		array(
			'key' => 'blog_type',
			'value' => 'clasic'
		),
		array(
			'key' => 'hits',
			'value' => array( 9900, 10000 ),
			'type' => 'numeric',
			'compare' => 'BETWEEN'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );
?>

I think, all above examples are sufficient. If any one have any issue then put your comments bellow.

Leave a Reply

Your email address will not be published. Required fields are marked *