Query meta

Get posts by meta:

$args = array(
	'meta_key' => 'my_key',
	'meta_value' => 'my_value',
	'meta_compare' => '!=' 
	// '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'
);
query_posts( $args );

Get posts with ordering by meta:

$args = array(
	'orderby' => 'meta_value',
	'order' => 'DESC' // ASC
);
query_posts( $args );

Get posts by multiple meta:

query_posts( array(
	'post_type' => 'catalog',
	'meta_query' => array(
		array(
			'key' => 'price',
			'value' => array( 10, 20 ),
			'compare' => 'BETWEEN',
			'type' => 'numeric',
		),
		array(
			'key' => 'type',
			'value' => 'square',
			'compare' => 'NOT LIKE'
		)
	)
) );

2 comments on “Query meta

Leave a Reply

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