Support

Account

Home Forums General Issues update_post hook question

Helping

update_post hook question

  • I’m using the following code to create title and slug from 2 custom fields.

    
    /** Create Title and Slug */
    function acf_title( $value, $post_id, $field ) {
     if ( get_post_type( $post_id ) === 'taktiko-mitroo' ) {
    
     $new_title = mb_strtoupper(get_field( 'doctors_fname', $post_id ), 'UTF-8' ) . ' ' . mb_strtoupper($value, 'UTF-8') ;
     $new_slug = sanitize_title( $new_title );
    
     wp_update_post(
     array(
     'ID' => $post_id,
     'post_title' => $new_title,
     'post_name' => $new_slug,
     )
     );
     }
     return $value;
    }
    add_filter( 'acf/update_value/name=doctors_lname', 'acf_title', 10, 3 );
    

    While it seems to work ok, i’m having a problem when I first create the post. It just shows one of the fields and not both as title. When I resave the post, it get both values.

    Am I missing something?

  • The problem is likely do to the fact that the second field is saved after the field named doctors_lname. So you are getting the new value for this field and the old value for doctors_fname.

    Try using acf/save_post with a priority of > 10 so that both fields are saved before you attempt to use them.

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

You must be logged in to reply to this topic.