How to get ACF values from custom post type?

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 uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

How to show confirmation alerts with OK and cancel buttons using Swift 5?SwiftAngular: Templates, directives, data binding, Services and dependency injectionAngularHow to Create a Backup From Any Database in SQL?SQL/MySQLHow to create a horizontal scrolling menu with and without flex using CSS (or SCSS)?CSS12 URLSearchParams methods in JavaScriptJavaScriptHow to Commit and Rollback Changes in SQL?SQL/MySQLIntroduction to components and templates Part 3: Data bindingAngularHow to draw an SVG path on scroll using JavaScript?JavaScriptHow to import MySQL small sample database into phpMySQL?SQL/MySQLHow to check if checkbox is checked or not using JavaScript?JavaScriptHow to change CSS with jQuery?CSSAvoid using new Array() in JavaScriptJavaScript