Set post title from two meta fields

In a custom post type I have post titles disabled, however in the list of posts and in the slug it just shows the title as “auto-draft”. I want to automatically take two pieces of post-meta and make then the title and the slug.

I thought this would work, but I can’t make it happen:

Read More
function set_event_title( $data , $postarr ) {
  if($data['post_type'] == 'events') {
    $event_date = get_post_meta($post_id,'event_datetime',true);
    $event_venue = get_post_meta($post_id, 'venue_name' , true);
    $event_title = $event_venue . ' - ' . $event_date;
    $post_slug = sanitize_title_with_dashes ($event_title,'','save');
    $post_slugsan = sanitize_title($post_slug);

    $data['post_title'] = $post_title;
    $data['post_name'] = $post_slugsan;
  }
  return $data;
}

add_filter( 'wp_insert_post_data' , 'set_event_title' , '99', 2 );

Anyone know how I might get it to do its thing correctly?

Related posts

Leave a Reply

2 comments

  1. You are using wrong variable on the following line:

    $data['post_title'] = $post_title;
    

    you should use $event_title in $post_title as following:

    $data['post_title'] = $event_title;
    

    Also Get Post ID from $postarr parameter.

    Updated Code :

    function set_event_title( $data , $postarr ) {
      if($data['post_type'] == 'events') {
        $event_date = get_post_meta($postarr['ID'],'event_datetime',true);
        $event_venue = get_post_meta($postarr['ID'], 'venue_name' , true);
        $event_title = $event_venue . ' - ' . $event_date;
        $post_slug = sanitize_title_with_dashes ($event_title,'','save');
        $post_slugsan = sanitize_title($post_slug);
    
        $data['post_title'] = $event_title;
        $data['post_name'] = $post_slugsan;
      }
      return $data;
    }
    add_filter( 'wp_insert_post_data' , 'set_event_title' , '10', 2 );
    

    For more information on this filter visit this page.

  2. I found that the previous answer, as stated in the comments, only works when saving an existing post and not new posts. The below will work for new posts and existing posts.

    // func that is going to set our title of our customer magically
    function w2w_customers_set_title( $data , $postarr ) {
    
        // We only care if it's our customer
        if( $data[ 'post_type' ] === 'w2w-customers' ) {
    
            // get the customer name from _POST or from post_meta
            $customer_name = ( ! empty( $_POST[ 'customer_name' ] ) ) ? $_POST[ 'customer_name' ] : get_post_meta( $postarr[ 'ID' ], 'customer_name', true );
    
            // if the name is not empty, we want to set the title
            if( $customer_name !== '' ) {
    
                // sanitize name for title
                $data[ 'post_title' ] = $customer_name;
                // sanitize the name for the slug
                $data[ 'post_name' ]  = sanitize_title( sanitize_title_with_dashes( $customer_name, '', 'save' ) );
            }
        }
        return $data;
    }
    add_filter( 'wp_insert_post_data' , 'w2w_customers_set_title' , '99', 2 );
    

    It first checks for the title in the $_POST variable, which is where it would come from most of the time. If it is not there, it will pull it from the post_meta, which will handle things like Quick Edits.