Yogesh Chauhan's Blog

WordPress: How to find all posts from a custom post type with specific custom field?

in WordPress on March 23, 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.


// args
$args = array(
	'numberposts'	=> -1,
	'post_type'	=> 'guest_posts',
	'meta_key'	=> 'blog',
	'meta_value'	=> 'yogeshchauhan'

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

<?php if( $the_query->have_posts() ): ?>
	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
			<a href="<?php the_permalink(); ?>">
				<img src="<?php the_field('img_thumbnail'); ?>" />
				<?php the_title(); ?>
	<?php endwhile; ?>
<?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
Observation of Human Behavior [Shopping Observation Example]MiscellaneousWhat are Modules and Components in Angular?AngularPostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.PostgresA complete guide to add responsive YouTube videos using HTML and CSSCSSThe Difference Between the echo and print Commands in PHPPHPUseful (and probably ignored) HTML tags: Part 1HTML@forward modules with a prefix in SCSS (Sass)SCSSWordPress: How to find all posts from a custom post type with multiple custom fields values?WordPressHow to change style of nth-child using jQuery?jQuerySelect statement in Postgres with examplesPostgresShould we ever delete data from a database?MiscellaneousHow to Make a Simple Module with a Form and Menu Link in Drupal 7.x?Drupal