Yogesh Chauhan's Blog

WordPress: How to find all posts from a custom post type with multiple custom fields values?

in WordPress on March 26, 2021

We’ll use same code as shown in this post to find all posts with a specific custom field value.

That is same as getting ACF values from custom post type.

This following example has complete code with WP_Query too.



<?php 

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

// query
$the_query = new WP_Query( $args );

?>

<?php if( $the_query->have_posts() ): ?>
<ul>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
  <li>
    <a href="<?php the_permalink(); ?>">
      <?php the_title(); ?>
    </a>
  </li>
  <?php endwhile; ?>
</ul>
<?php endif; ?>

<?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>


Credit: ACF Docs


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
5 Ways to Loop Through JavaScript ArraysJavaScriptPHP arrays for US states’ full names and abbreviationsPHPHow to implement NSNumberFormatter in Swift for currency?Swift2 Ways we can create an Array in JavaScriptJavaScriptLearn to Implement Estimated Reading Time using PHP Part 2: Final Implementation with Source CodePHPKanban vs Scrum: The two frameworks of agile principlesMiscellaneousHow to apply style to the first-child using jQuery?jQueryThe actual difference between indexOf() and search() in JavaScriptJavaScriptHow to zoom an image inside a box on hover?CSSForcing the domain to serve securely using HTTPSMiscellaneousHow get_template_part helps write reusable code in WordPress?WordPressSQL Left JoinSQL/MySQL