Support

Account

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

  • Update:
    The above answer will allow you to update previously established posts but not work for new posts, so the following function addresses that and allows for new posts and previous posts to modify the title from acf fields:

    
    function update_contacts_title($post_id) {
    
    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 );
        
    
    }
    add_filter('acf/save_post', 'update_contacts_title', 10, 3);