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 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 add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
What Is a Graph Database?MiscellaneousHow to get the first element with a class name xyz using JavaScript?JavaScriptAttributes and Properties in JavaScript and HTMLHTMLHow to remove WordPress TinyMCE Editor buttons?WordPressHow to add a ribbon inside a container using CSS?CSSWhat is a component helper function in React?ReactHow to create a secure random number using JavaScript?JavaScriptHow to host Google fonts on your server and add them using CSS?CSSDid you get an error while displaying Special Characters in JavaScript?JavaScriptObservation of Human Behavior [Shopping Observation Example]MiscellaneousWhat is Zdog?MiscellaneousINTERSECT and EXCEPT in PostgresPostgres