YogeshChauhan . com

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to create flickering text using CSS?CSSJavaScript Input Validation Theme Park Example using throw StatementJavaScriptWhen you don’t want to @forward every member in SCSS (Sass)SCSSDISTINCT ON: The confusing, unique and useful feature in PostgresPostgresWhat is Git?MiscThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresUnderstand Inheritance in PHP in this Basic Example (For Beginners)PHPShould we ever delete data from a database?MiscHow to set default timezone using PHP?PHPWordPress: How to get field values in Advanced Custom Fields?WordPressWordPress: How to setup and get values from an ACF options page?WordPressWhat are Conditional Tags in WordPress?WordPress