skip to Main Content

I have a code that changes post data and replaces them with the data of a json file:

add_filter( 'wp_insert_post_data', 'update_post',10 ,3 );

function update_post( $data, $postarr ) {
    $postid = $postarr['ID'];
    $imdbl = get_field('imdbl', $postid);
    if ($imdbl) {
        $url = 'http://www.omdbapi.com/?i='.$imdbl.'&apikey=c0ceddac';
        $jdata = file_get_contents($url);
        $json = json_decode($jdata);
        $data['post_title'] = $json->Title;

        update_field('ReleaseY', $json->Year, $postid);

    }

    return $data;

}

Now, this code works, it gets the Title and puts it in posts title. My problem is with this part of the code:

update_field('ReleaseY', $json->Year, $postid);

This code replaces the value of a custom field, it works perfectly on a ‘Post’ post type but in other post types (in this case ‘movies’), it doesn’t work at all.

I don’t know what the problem is and I searched all over the Internet and I tried everything.

Please help me!!

Thank you.

2

Answers


  1. Try using the native WP function update_post_meta instead, unless you’re sure ACF has loaded at the time you’re trying to use the function:

    update_post_meta( $postid, 'ReleaseY', $json->Year );
    

    Reference: https://developer.wordpress.org/reference/functions/update_post_meta/

    Login or Signup to reply.
  2. I just changed:

    update_field('ReleaseY', $json->Year, $postid) 
    

    To:

    update_post_meta($postid,'ReleaseY', $json->Year) 
    

    It looks good now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search