How to take custom field value (ACF plugin) to WordPress save_post hook?

I use Advance Custom Fields plugin to create some custom fields.
While creating new post or update post, I need to use api (vimeo) to take values to other custom field.

Example:

Read More
  • 1 field: ID of video
  • 2 field: duration
  • 3 field: plays count

I enter value in first field, press “Publish” and use this hook:

add_action( 'save_post', 'vimeo_api', 10, 2 );
function vimeo_api( $post_id, $post ) {
  // request to vimeo with video ID
  update_post_meta( $post_id, 'video-duration', $vimeo_single['body']['duration']);
  update_post_meta( $post_id, 'video-plays', $vimeo_single['body']['stats']['plays'] );
}

If I hard-code vimeo ID – it works!

But I can’t get value from field 1.

For example I can get value of post title like this:

$title = $post->post_type;

But it doesn’t works with ACF field.

In developer tools I see this in “headers” tab, form data:

_wpnonce:83ab5bcf5f
_wp_http_referer:/wp-admin/post.php?post=37&action=edit&message=1
user_ID:1
action:editpost
originalaction:editpost
post_author:1
post_type:video
...
fields[field_5423b0bb92209]:
fields[field_5423aff492207]:103222207
fields[field_5423b04192208]:
fields[field_5424dd92c4f3d]:

This return error Warning: Illegal string offset:

$vimeo_id = $post->fields['field_5423aff492207'];

Related posts

Leave a Reply

1 comment

  1. Solved with acf/save_post hook

    function get_video_info_from_vimeo ($post_id) {
    
    $vimeo_id = get_field('field_5423aff492207', $post_id); // get id
    
    // use api and get $duration and $plays
    
    // unhook this function so it doesn't loop infinitely
    remove_action('acf/save_post', 'get_video_info_from_vimeo');
    
    // update ACF Price Field
    update_field( 'field_5423b04192208', $duration, $post_id );
    update_field( 'field_5424dd92c4f3d', $plays, $post_id );
    
    // re-hook this function
    add_action('acf/save_post', 'get_video_info_from_vimeo');
    
    }
    
    add_action( 'acf/save_post', 'get_video_info_from_vimeo' );