I have posts with custom fields created by acf. These fields get filled by external code which works so far. See Screenshot #1.
I can access the values via get_post_meta but not with get_field. get_field returns nothing. I think because the field_key relationships are missing. when i open one post in wp-admin and just click “update” without changing anything, get_field now returns the values. i try to fix this by a bulk code instead of opening/saving each post by hand:
add_action( 'wp', 'batch_save_posts' );
function batch_save_posts() {
$get_posts = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => "werkstatt"
));
while($get_posts->have_posts()) :
$get_posts->the_post();
$fields = array(
"anschrift" => "field_5be4355066b72",
"plz" => "field_5be4355e66b73",
"ort" => "field_5be4356866b74",
"telefon" => "field_5be4356f66b75",
"email" => "field_5be4357866b76",
"website" => "field_5be4357f66b77"
);
foreach($fields as $key=>$field){
if($value = get_post_meta(get_the_ID(),$key)){
update_field($field,$value[0],get_the_ID());
}
}
write_log("updated: ".get_the_title());
endwhile;
wp_reset_query();
}
when loading wp, all fields get the field key relationships as you can see in screenshot #2. but still i am not able to retrieve the field values with get_field. still have to save the post once to get this working. what do i miss here?!
I have a similar problem – would like to bulk edit all my posts. Did you managed to get it work? Thanks!
What you are using to import the values needs to create the correct field key reference when the field is updated. This is not something easily done after the fact.
Thanks, John! nicmare asked the question “still have to save the post once to get this working. what do i miss here?!” is this related to my wish that I want to find a way to change some posts in a way that I export them, incl the proper (field value, fiel key) pairs; modify some values and finally import them back? What do I miss there?
The topic ‘get_post_meta works but get_field not’ is closed to new replies.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.