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 ' ';
foreach($posts as $post)
{
echo '- ' . get_the_title($post->ID) . '
';
}
echo '
';
}
The example above will return all posts from custom post type event that has a location = chicago.
ACF advanced custom examples post type values