0

I'm trying to set the value of custom fields when a custom post type ('film') is published or updated. The end goal is to get meta-data from an API based on a URL field when the post is published or updated.

I tried to use add_action( 'save_post_film', 'get_film_data', 10, 2 );, with the following function :

function get_film_data( $post_id ) {

    // If this is just a revision, don't send the email.
    if ( wp_is_post_revision( $post_id ) )
        return;

    $value = 'something'; // The value depends in fact on the value of another field
    update_post_meta($post_id, 'some_custom_field', $value);
}

This only works when a new post is created. The custom field's value is already set when the edit form opens. But for some reason it doesn't work once the post has been published.

What am I doing wrong ?

  • On top of my head this should totally work, save hooks fire pretty much always (outside of some edge cases like scheduled posts). – Rarst Nov 2 '17 at 13:55
  • For some reason the hook I used (for custom post types) doesn't work the same way as save_post. Could be a bug ? – GuitarExtended Nov 2 '17 at 14:50
  • That would be very strange since the two hooks are right next to each other in code and are pretty much identical outside of name. I can't think of how it would possibly break in this way really. Probably impossible to say without hands on debug. – Rarst Nov 2 '17 at 14:53
0

It looks to me like save_post doesn't work the way you currently have your add_action.

According to the Codex, you need to run your function on save_post and check the post type in the function.

function get_film_data( $post_id ) {

   if ( get_post_type($post_id) == 'film' ) {

        // If this is just a revision, don't send the email.
        if ( wp_is_post_revision( $post_id ) )
        return;

        $value = 'something'; // The value depends in fact on the value of another field
        update_post_meta($post_id, 'some_custom_field', $value);

    }

}
add_action( 'save_post', 'get_film_data', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.