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'

	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 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
How to convert a number rounding to a specified number of decimals in JavaScript?JavaScriptFunction Scope in JavaScript for BeginnersJavaScriptVariables and Distance Functions in SCSS (Sass)SCSSValues and Types Basics in JavaScriptJavaScriptHow to force your website to load securely with an .htaccess file?MiscellaneousHow to enable GD library support for PHP on windows server?PHPResponsive Masonry Grid using CSS columns PropertyCSSaddcslashes() and addslashes() String Functions in PHPPHPLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPHow to use @supports rule in CSS?CSSHigher Order Functions in JavaScript with ExamplesJavaScriptHow to Block IPs and User Agents using code in Drupal or WordPress?Drupal