Support

Account

Home Forums General Issues Update custom post title with ACF Fields Reply To: Update custom post title with ACF Fields

  • Here’s the answer:

    
    function update_contacts_title($value, $post_id, $field) {
    
    if( have_rows('contact_info') ):
        while( have_rows('contact_info') ): the_row(); 
    
            $first_name = get_sub_field('first_name', $post_id);
            $last_name = get_sub_field('last_name', $post_id);
            $title = $first_name . '-' . $last_name;
            $slug = sanitize_title( $title );
    
        endwhile;
    endif;
    
        $postdata = array(
            'ID'          => $post_id,
            'post_title'  => $title,
            'post_type'   => 'contacts',
            'post_name'   => $slug
        );
    
        wp_update_post( $postdata );
        return $value;
    
    }
    add_filter('acf/update_value/name=last_name', 'update_contacts_title', 10, 3);