Some time we have to show posts, pages custom posts after doing some filtering on basis of some particular field. Here we assume that you already have basic knowledge about WP_Query or get_posts class in WordPress. First of all we will filter the posts by simple one field.

1. Single Meta key Filter
Here you we have will get posts whose meta key “diplay_on_homepage” has value of “true”.

// the meta_key 'diplay_on_homepage' with the meta_value 'true'
$cc_args = array(
	'posts_per_page'   => 5,
	'post_type'        => 'post',
	'meta_key'         => 'diplay_on_homepage',
	'meta_value'       => 'true'
);
$cc_query = new WP_Query( $cc_args );

You can also do same thing with these array format :

// the meta_key 'color' with the meta_value 'blue'
$cc_args = array(
	'posts_per_page' => 5,
	'post_type'      => 'post',
	'meta_query'     => array(
		array(
			'key' => 'color',
			'value' => 'blue'
		)
	)
);
$cc_query = new WP_Query( $cc_args );

1. Meta Query with Numeric Meta Values
When we have to do filter on numeric basis, we can also do this with some minor addition of code.
Here is one example for the shop purpose. In this we have assumed that we have created custom post type or WooCommerce post type “product”.

For getting products whose price is 1000 and more…

// the product price is 1000 and more
$cc_args = array(
	'posts_per_page' => 10,
	'post_type'      => 'product',
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => 1000,
			'type' => 'numeric',
			'compare' => '>='
		)
	)
);
 
$cc_query = new WP_Query( $cc_args );

For getting products whose price is less than 1000

// the product price is 1000 and more
$cc_args = array(
	'posts_per_page' => 10,
	'post_type'      => 'product',
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => 1000,
			'type' => 'numeric',
			'compare' => '<'
		)
	)
);
 
$cc_query = new WP_Query( $cc_args );

Now If we want the product between two specific range, we can get it by passing desired value in array format.

// the product price is more than 1000 and less than 2000
$cc_args = array(
	'posts_per_page' => 10,
	'post_type'      => 'product',
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => array( 1000, 2000 ),
			'type' => 'numeric',
			'compare' => 'BETWEEN'
		)
	)
);
 
$cc_query = new WP_Query( $cc_args );

3. Meta Query with Two or more Meta Keys
Just take an example for Shop website, if you want to show the products on Home page whose is on sale also.

// the product is on sale and enabled for showing on home page
$cc_args = array(
	'posts_per_page' => 10,
	'post_type'      => 'product',
	'meta_query' => array(
		'relation' => 'AND',
		array(
			'key' => 'diplay_on_homepage',
			'value' => 'true'
		),
		array(
                        'key'   => 'on_sale',
			'value' => 'true'
		),
	)
);
$cc_query = new WP_Query( $cc_args );

You can also apply more filters to the meta query. Here we have added more condition like whether the product is available or not.

// the product is on sale and enabled for showing on home page
$cc_args = array(
	'posts_per_page' => 10,
	'post_type'      => 'product',
	'meta_query' => array(
		'relation' => 'AND',
		array(
			'key' => 'diplay_on_homepage',
			'value' => 'true'
		),
		array(
                        'key'   => 'on_sale',
			'value' => 'true'
		),
		array(
                        'key'   => 'is_available',
			'value' => 'yes'
		),
	)
);
$cc_query = new WP_Query( $cc_args );

We can also apply some complex query with Meta query also.

// the 'color' is 'blue' AND the 'price' is more than 1000 and less than 2000 and is enabled for Home page also
$cc_args = array(
	'meta_query' => array(
	  'posts_per_page' => 10,
	  'post_type'      => 'product',
		'relation' => 'AND',
		array(
			'key' => 'diplay_on_homepage',
			'value' => 'true'
		),
		array(
			'relation' => 'AND',
			array(
				'key' => 'color',
				'value' => 'blue'
			),
			array(
				'key' => 'price',
				'value' => array( 1000, 2000 ),
				'type' => 'numeric',
				'compare' => 'BETWEEN'
			)
		)
	)
);
 
$cc_query = new WP_Query( $cc_args );
There are currently no comments.