Support

Account

Home Forums ACF PRO get_post_meta works but get_field not

Solving

get_post_meta works but get_field not

    • nicmare

    • November 15, 2018 at 2:43 am

    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?!

    • BaSa69

    • July 21, 2020 at 12:35 am

    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.

    • BaSa69

    • July 27, 2020 at 3:23 am

    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?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

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.