Home › Forums › Front-end Issues › get_field arbitrarily not returning value
Using ACF PRO 5.5.7 with WP 4.7.2. Rolling back to ACF 5.5.3 does not fix.
I have a function to which I pass a $post object and have simplified the function to isolate the problem:
printLinkBar($post) {
echo '<pre>';
echo '<h2>' . $post->ID . '</h2>';
$source = get_field('source', $post->ID);
echo 'source: ' . $source . '<br>';
$source2 = get_field('field_561ef519ecce4', $post->ID); // Key refers to 'source' field
echo 'source2: ' . $source2 . '<br>';
$all = get_fields($post->ID);
var_dump($all);
echo '</pre><hr>';
return;
}
I call this function repeatedly and not from within the_loop. It is always passed a post that is a custom post type.
The first time it is called, it does not output the value for the ‘source’ field and the field is missing in the value returned by get_fields.
When a newer post is created, that newer post will not have these values output and the post that didn’t work works.
The value of the field always shows up as expected in the admin.
Calling setup_postdata() doesn’t fix things.
Changing the name of the field to source2 or source3 does not fix the problem.
Simply visiting the post in the admin *sometimes* fixes it, but not always. This post type is created outside of the admin so usually they are never seen within the admin.
How are the fields set/updated in not in the admin? Example of code used to update the fields?
Do you have a caching plugin or some other caching system running on the site?
Updating to 5.5.8 – released today – seems to have fixed the issue, but answering anyway:
Registered users can create CPTs from a form on the front end. The line that attaches the custom field to the CPT is
update_field('field_561ef519ecce4', $_REQUEST['directory-link-source'], $post_id;
The site uses W3 Total Cache for caching but this problem happened when the plugin was disabled, however after I disable W3TC, I must disable ACF Pro before I am able to re-activate W3 TC.
I’ve spent way too much time on this issue to further nail down exactly what was going on. I’m just crossing my fingers. But if I can help solve a problem for someone else, I would be happy to to some tests to further isolate the problem.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
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.