Yogesh Chauhan's Blog

How to get ACF values from custom post type?

in WordPress on February 24, 2021

Advanced Custom Fields makes the page really fancy and the good thing is that we can use those fields in custom post types too. Though, it’s a bit tricky to get all those field values from the custom post types.

We can use get_posts() function in WordPress and query the meta to get ACF fields.

We saw tons of examples on how to use get_posts() function, let’s try those queries on ACF fields.

Let’s take a look at the example of finding Events (post type) where location (custom field – select) equals Chicago (value).



$posts = get_posts(array(
	'numberposts' => -1,
	'post_type' => 'event',
	'meta_key' => 'location',
	'meta_value' => 'chicago'
));

if($posts)
{
	echo '<ul>';

	foreach($posts as $post)
	{
		echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
	}

	echo '</ul>';
}


The example above will return all posts from custom post type event that has a location = chicago.


amazon

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 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
INNER JOIN in PostgresPostgresHow to Create a Backup From Any Database in SQL?SQL/MySQLLearn to make a responsive gallery using CSS Grid and without media queriesCSSHow to create a smooth scrolling effect with CSS?CSSHow to create a sidebar using pure CSS?CSSWhat does useEffect do in React?ReactHow to add Laravel to WordPress using Sage theme (and install Tailwind CSS)?PHPHow to implement NSNumberFormatter in Swift for currency?SwiftHow to get the height and width of an element using JavaScript?JavaScriptClasses in JavaScript: The BasicsJavaScriptAll different methods for accessing elements in the DOM using JavaScriptJavaScriptWhat is PostgreSQL? How similar or different it is from SQL?Postgres