Query or Exclude Posts by Custom Field Data


Categories
Tags
A few examples of how to manipulate WordPress post queries using Custom Fields.

Photo by Launchpresso on Unsplash

Get Posts with a Custom Field

Get all posts where the custom field called ‘country’ is equal to ‘mexico’:

$args=array(
	'numberposts' => '-1',
	'meta_key' => 'country',
	'meta_value' => 'mexico',								
); $query = new WP_Query( $args );

Sort Posts with a Custom Field

An example of how you could use custom fields to create a custom numerical sort order for your posts, using custom fields. The example below would get up to 100 posts from the ‘blog’ category that have a value present in a custom field called ‘sort’, these are ordered by meta_value_number (so 0,1,2,3,4 etc), then by ASC (ascending) order.

To sort your posts simply add numbers as values in the ‘sort’ custom field. So for whichever post you want to show first you can enter ‘0’ as the value. If you have multiple posts with the same numerical value for the sort value (e.g. 3 posts with 0 for sort) then these posts will be ordered in ascending order, followed by sequential ordering – posts sorted by 1,2,3,4 etc.

$args=array(
	'category_name' => 'blog',
	'orderby' => 'meta_value_num',
	'meta_key' => 'sort',
	'order' => 'ASC',
	'posts_per_page' => 100,								
); $query = new WP_Query( $args );

Exclude Posts with a Custom Field

To show posts excluding a value in the ‘sort’ custom field you can use meta_query. ‘Key’ specifies the custom field and the ‘compare’ value of ‘NOT EXISTS’ tells WordPress to get all posts from the ‘blog’ category excluding those with a value in the ‘sort’ custom field:

$args=array(
	'category_name' => 'blog',
	'meta_query' => array(
		array(
			'key'     => 'Sort Order',
			'compare' => 'NOT EXISTS',
		),
	),								
); query_posts($args);

Comments

Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the author(s) and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views. Nothing negative please.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).

Thank you – we hope you find the website useful.