Support

Account

Home Forums Backend Issues (wp-admin) automatically change title to ACF field value

Solving

automatically change title to ACF field value

  • I have a custom plugin that defines a custom post type. Upon creating a new post of that type, I want the title field to be automatically filled based on one of my advanced custom fields. I have this working, however it will only currently work on update and not the first time it inserts/publishes. Can anyone tell me why?

    add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 );
    /**************************************************************
    /* automatically title snow reports based on date 
    /*************************************************************/
    function modify_post_title( $data , $postarr ){
      if($data['post_type'] == 'gtge_snow_report') {
      	
        $id = $postarr['ID'];
        $data['post_title'] = get_post_meta($id, 'date_chief_joe', true);
      }
      
      return $data;
    }
  • get_post_meta retrieves the meta values of a saved post, but since your post hasn’t been saved yet, this data isn’t retrievable from the DB (it’s not there yet). I’m trying to do this exact same thing myself with wp_insert_post_data, but I’m also having problems.

  • //add title and alias when a person is created
    function my_acf_update_titlevalue( $value, $post_id, $field ) {
    global $post;
    	// vars (title comes from person_name field that is looked for at the filter, after that the alias is generated)	
    	$new_title = $value;
    	$new_slug = sanitize_title( $new_title );
    
    	// update post with the "new" title and alias (because we may hide them at our custom post)
    	$my_post = array('ID'=> $post_id,'post_title' => $new_title,'post_name' => $new_slug );
    
    wp_update_post( $my_post );
    
    return $value;
    }
    
    add_filter('acf/update_value/name=person_name', 'my_acf_update_titlevalue', 10, 3);
    

    try something like above at your plugin: change person_name to your acf field

  • Hi,
    Sorry to bring up an old thread but I am using this code and it works well for standard text fields but I am trying to change the title to a Taxonomy field. The problem is that it changes my title to the ID and not the taxonomy name. Any ideas on how I can get it to display the name?

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

The topic ‘automatically change title to ACF field value’ is closed to new replies.