YogeshChauhan.com

WordPress: How to find all posts with a specific custom field value?

March 19, 2021

We can pass arguments in get_posts function and WP_Query Object and query custom field values.

Here’s a basic example of how to use get_posts function and pass the arguments to find all posts where a custom field called “blog” has a value of “yogeshchauhan”.



$posts = get_posts(array(
	'numberposts'	=> -1,
	'post_type'	=> 'posts',
	'meta_key'	=> 'blog',
	'meta_value'	=> 'yogeshchauhan'
));


Here’s another basic example of how to use get_posts function and pass the arguments to find all posts from custom post type “guest_posts” where a custom field called “blog” has a value of “yogeshchauhan”.



$posts = get_posts(array(
	'numberposts'	=> -1,
	'post_type'	=> 'guest_posts',
	'meta_key'	=> 'blog',
	'meta_value'	=> 'yogeshchauhan'
));


Custom Fields Query like SQL: AND Operator

In the following example, I am passing multiple arguments in get_posts function to find all posts where a custom field called “blog” has a value of “yogeshchauhan” or “w3org”.

Also, there is another custom field called “featured” which is a checkbox that indicates if the blog post if featured or not and it takes boolean parameter. We are passing 1 as a parameter to see whether that checkbox is checked.



$posts = get_posts(array(
	'numberposts'	=> -1,
	'post_type'		=> 'post',
	'meta_query'	=> array(
		'relation'		=> 'AND',
		array(
			'key'	 	=> 'blog',
			'value'	  	=> array('yogeshchauhan', 'w3org'),
			'compare' 	=> 'IN',
		),
		array(
			'key'	  	=> 'featured',
			'value'	  	=> '1',
			'compare' 	=> '=',
		),
	),
));


Custom Fields Query like SQL: OR Operator

We can convert the same example from above AND operator and just change it to OR operator example.



$posts = get_posts(array(
	'numberposts'	=> -1,
	'post_type'		=> 'post',
	'meta_query'	=> array(
		'relation'		=> 'OR',
		array(
			'key'	 	=> 'blog',
			'value'	  	=> array('yogeshchauhan', 'w3org'),
			'compare' 	=> 'IN',
		),
		array(
			'key'	  	=> 'featured',
			'value'	  	=> '1',
			'compare' 	=> '=',
		),
	),
));


Credit: ACF Docs

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

Aggregate Functions Examples in Postgres for BeginnersPostgresJavaScript Data Types and Data Structures Things to RememberJavaScriptHow to check if the page is single post page in WordPress?WordPressHow to convert datetime to date format in JavaScript?JavaScriptSQL ANY OperatorSQL/MySQLAngular 9 time clock update every minute, second, hourAngular