The following method is causing me some trouble.
function populate_acf_field_options_from_cpt($post_type, $acf_field, $destination_field)
{
$posts = get_posts(array(
‘post_type’ => $post_type, //retrieve posts
‘posts_per_page’ => -1 // retrieve all posts
));
foreach ($posts as $post) {
$value = get_field($acf_field, $post->ID);
error_log($value);
}
// Reset post data
wp_reset_postdata();
}
I have a custom post type called “locations” that is used to present information about business locations. Each post has some ACF fields used to render information on the corresponding post. I also have another custom post type “providers” that is meant for each staff member. As locations are dynamic my hope was to loop through the location posts, get the value from a field or fields and use that information to populate a checkbox on the providers posts dynamically.
The method above is simplified for testing because I’ve been having issues.
The method I provided works as expected, I am able to access the information and the error_log is spitting out what I need.
However, when I go to edit a post, any post of page with ACF fields they are missing. I have been hacking at this for a while and the only way to get the fields to return is to remove the get_field() from the loop.
I’m at a loss.
Thank you for any help.