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:
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?
You are using wrong variable on the following line:
you should use $event_title in $post_title as following:
Also Get Post ID from $postarr parameter.
Updated Code :
For more information on this filter visit this page.
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.
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 thepost_meta
, which will handle things like Quick Edits.