Home › Forums › General Issues › CPT ACF Fields not working with ID parameter
I’m trying to pull ACF fields from related posts for a custom post type named Locations I’ve created. The fields appear without problem within the post itself when an ID is not specified.
I’ve tried retrieving the field directly with an ID in the single-locations.php file inside and outside the content loop with no luck.
This code snippet below (for testing purposes) is currently inside of an ACF block template file that is added to the post type.
<?php
global $post;
$id = get_the_ID();
the_field('display_address'); //this works
echo $id; // returns the custom post ID
var_dump($post);
the_field('display_address', $id); //this does not.
?>
post dump:
object(WP_Post)#1254 (24) { [“ID”]=> int(1109) [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2019-11-15 18:53:46” [“post_date_gmt”]=> string(19) “2019-11-15 18:53:46” [“post_content”]=> string(2470) “ ” [“post_title”]=> string(8) “East Bay” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish” [“comment_status”]=> string(6) “closed” [“ping_status”]=> string(6) “closed” [“post_password”]=> string(0) “” [“post_name”]=> string(8) “east-bay” [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “” [“post_modified”]=> string(19) “2019-11-16 19:22:16” [“post_modified_gmt”]=> string(19) “2019-11-16 19:22:16” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> int(0) [“guid”]=> string(54) “http://localhost:8888/?post_type=locations&p=1109” [“menu_order”]=> int(0) [“post_type”]=> string(9) “locations” [“post_mime_type”]=> string(0) “” [“comment_count”]=> string(1) “0” [“filter”]=> string(3) “raw” }
I saw somewhere in my internet searching that CPT have their name prepended to the ID. I’ve tried several syntactically different ways of combining the two with no luck, also.
Any insight on this would be much appreciated because I have no idea why this wouldn’t be working.
Thank you,
Alex
After some digging through the ACF API files, I tested the acf_get_valid_post_id
function. When no ID is passed into the function, the ID that gets returned looks something like block_[RANDOM STRING]
. When an ID is passed into the function, the ID that was passed in just gets returned.
When I use that block string as the ID parameter, everything actually works as expected. My current predicament now is retrieving those block string keys dynamically through the wordpress post ID because currently, I can only get those keys when no post ID is provided (So it only works on the specific post page itself).
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.