YogeshChauhan.com

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

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

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

Full and Partial ROLLUP in Postgresql with ExamplesPostgres5 Key Principles Of Good Website UsabilityUI/UXFile System Integrity: How to Keep an Eye on Your Files and Folder Change?MiscINTERSECT and EXCEPT in PostgresPostgresHow to make WordPress main stylesheet (style.css)?WordPressSome Useful Operators in The SQL WHERE ClauseSQL/MySQLHow to create a cross-browser smooth scrolling with jQuery?jQueryWordPress: How to get field values in Advanced Custom Fields?WordPress4 ways to create Date Objects in JavaScriptJavaScriptHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQLSafe Practice to add Links to cross-origin destinationUI/UXUPDATE and DELETE Statements in PostgresPostgres