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?

  • I was trying to accomplish something similar. Here is how I did it.

    My PHP is rusty so if you spot any improvements please do let me know.

    I wanted to populate my post title to combine two custom fields: first-name and last-name

    function custom_field_value_as_title( $value, $post_id, $field ) {
    	global $_POST;
    	// vars
    
    	$new_title_first_name = get_field('first-name', $post_id);
    	$new_title_last_name = get_field('last-name', $post_id);
        $new_title = "$new_title_last_name, $new_title_first_name";
    	//$new_slug = sanitize_title( $new_title );
    
        $result = 
    
    	// update post
    	// http://codex.wordpress.org/Function_Reference/wp_update_post
    	  $my_post = array(
          'ID'         => $post_id,
          'post_title' => $new_title,
    	  'post_name'  => $post_id //originally $new_slug
      );
    
    // Update the post into the database
      wp_update_post( $my_post );
    }
    
    add_filter('acf/update_value', 'custom_field_value_as_title', 10, 3);

    The result in the post title is last-name, first-name. For example: “Obama, Barack”