WordPress WP_Query: Get Posts by Meta Values

It is the most useful thing when you create post filters or advanced search on your WordPress blog / WooCommerce shop.

#metas, #WP_Query  /   44

In this post I assume that you already have basic knowledges how to work with WP_Query class in WordPress. Before I begin, I want to show you some very simple examples. The similar examples you can find in WordPress Codex.

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_on_homepage and meta value on, you can do it in the following way:

$rd_args = array(
	'meta_key' => 'show_on_homepage',
	'meta_value' => 'on'
);
 
$rd_query = new WP_Query( $rd_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:

$rd_args = array(
	'meta_key' => 'show_on_homepage',
	'meta_value' => 'on',
	'meta_compare' => '!='
);
 
$rd_query = new WP_Query( $rd_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 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».

// the meta_key 'color' with the meta_value 'white'
$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'color',
			'value' => 'white'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

If you look in the post edit page (in admin area) of any queried posts, you will see the following in the «Custom Fields» section:

the meta_key and the meta_value

Get all the posts except the ones with meta key «color» and meta value «white»:

$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'color',
			'value' => 'white',
			'compare' => '!='
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Now let’s get all the posts with white OR green color custom field value:

// custom field name is color and custom field value is 'white' OR 'green'
$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'color',
			'value' => array('white','green'),
			'compare' => 'IN'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Get all the posts (products in online shop for example) except white products and green products:

$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'color',
			'value' => array('white','green'),
			'compare' => 'NOT IN'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Get Posts Within a Given Range of Numeric Meta Values

For example, let’s filter the online shop products by prices:

// the product price is more than 2000 and less than 4000
$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => array( 2000, 4000 ),
			'type' => 'numeric',
			'compare' => 'BETWEEN'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Numeric Comparison

The product price in this example is 2000 or more than 2000:

$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => 2000,
			'type' => 'numeric',
			'compare' => '>='
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

The product price is less than 4000:

$rd_args = array(
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => 4000,
			'type' => 'numeric',
			'compare' => '<'
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Query Posts by Several (two or more) Custom Field Values

Let’s combine some of the previous examples:

// the 'color' is 'white' AND the 'price' is more than 2000 and less than 4000
$rd_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'
			)
		)
	)
);
 
$rd_query = new WP_Query( $rd_args );

Only the best of WordPress

Subscribe to this weekly newsletter to receive the latest blog posts by email.I respect your privacy. Your email is safe with me.

Comments 44

← Older
  • Misha,
    This is my mySQL code below, how do I get an array with the same values using wordpress?

    SELECT A.Product, C.MemberPrice, CONCAT(WPP.post_title, ': $', C.MemberPrice) AS MemberTitle
    FROM wp_posts WPP 
    LEFT JOIN (SELECT post_id, meta_value AS Product FROM wp_postmeta
    WHERE post_id IN( 361,362,364,365) AND meta_key IN('_sku')) A ON WPP.id = A.post_id 
    LEFT JOIN (SELECT post_id, CAST(meta_value AS DECIMAL(18,2)) AS MemberPrice FROM wp_postmeta
    WHERE post_id IN( 361,362,364,365) AND meta_key IN('_sale_price')) C ON WPP.id = C.post_id
    WHERE WPP.Id IN(361,362,364,365)

Leave your question or feedback

phpjsHTMLCSSSQLCode
Please, enter a comment
Please, enter a name
Incorrect email