Support

Account

Home Forums Backend Issues (wp-admin) Replacing custom post type post title with an acf? Reply To: Replacing custom post type post title with an acf?

  • A variation of what has been posted worked for us. Unlike others we seemed to be getting trapped in an infinite loop. Here is a solution derived from some information on the WP codex. Maybe it can help you out:

    function person_update_title( $value, $post_id, $field ) {
    	
    	$new_title = get_field( 'person_first_name', $post_id) . ' ' . $value;
    	$new_slug = sanitize_title( $new_title );
    	
    	// update post
    	$person_postdata = array(
    		'ID'          => $post_id,
    		'post_title'  => $new_title,
    		'post_name'   => $new_slug,
    	);	
    	
    	if ( ! wp_is_post_revision( $post_id ) ){
    	
    		// unhook this function so it doesn't loop infinitely
    		remove_action('save_post', 'person_update_title');
    	
    		// update the post, which calls save_post again
    		wp_update_post( $person_postdata );
    
    		// re-hook this function
    		add_action('save_post', 'person_update_title');
    	}	
    	
    	return $value;
    }
    
    add_filter('acf/update_value/name=person_last_name', 'person_update_title', 10, 3);

    (If this is a duplicate, please kill it. Having troubles navigating the interwebs)