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.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Learn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPWordPress: How to query all posts from custom post type and display them in a list?WordPressHow to use $IF operator in Envision Basic?Envision BasicWordPress: How to add a Search Icon in Menus with toggle effect using jQuery?jQueryHow to make WordPress main stylesheet (style.css)?WordPressThe Difference Between isNaN() Method And isNaN() Function In JavaScriptJavaScriptWhat is Prototypal Inheritance in JavaScript?JavaScript4 different ways to create JavaScript ObjectsJavaScriptHow does AdSense calculate page loading time?JavaScriptCanvas Drawing in HTML5HTMLA list of wp-cli commands to use via SSHWordPressHow to avoid element shift on border change while hovering in CSS?CSS