I have a custom post type without title support and I'm trying to generate one from post's taxonomies and custom fields. To do so, I'm using this code:

function custom_post_type_title_filter( $data , $postarr )
      if ($postarr['post_type'] == 'cars'){
            $id = get_the_ID();
            $engine= ', '.get_post_meta($id, 'Engine', true).'l';
            $terms = wp_get_object_terms($id, 'brand');
            $abrand= ' '.$terms[0]->name;
            $amodel = ' '.$terms[1]->name;
            $data['post_title'] = $id.$abrand.$amodel.$engine;
      return $data;
    add_filter( 'wp_insert_post_data' , 'custom_post_type_title_filter' , 99, 2 );

Problem is, to make it work, I have to republish my post. It does not work with new post (it has no ID yet, I guess) and if I change custom field values in post, it won't generate name from them yet, I'll have to save it twice to get it done.

Can someone share a solution with example how it's done properly?

Also, would be nice to be able to set custom slug too (different from the title).

2 Answers 2


You can try the following code.

function custom_post_type_title ( $post_id ) {
    global $wpdb;
    if ( get_post_type( $post_id ) == 'cars' ) {
        $engine= ', '.get_post_meta($post_id, 'Engine', true).'l';
        $terms = wp_get_object_terms($post_id, 'brand');
        $abrand= ' '.$terms[0]->name;
        $amodel = ' '.$terms[1]->name;
        $title = $post_id.$abrand.$amodel.$engine;
        $where = array( 'ID' => $post_id );
        $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
add_action( 'save_post', 'custom_post_type_title' );
  • 1
    It works! Just had to set priority to 100 to work with Custom Field Template plugin. Is there any way to set custom slug too?
    – Kovas
    Commented Jun 14, 2011 at 21:08
  • 1
    Yes you can set the custom slug too. You just have to update the post_name instead of post_title. But beware changing slug on every save is not a good idea, so you might want to generate the custom slug first time and save it in custom field, otherwise your posts URL will change with every update/save. Commented Jun 15, 2011 at 5:39
  • 1
    well, slug will look something like id-tax_term-tax_subterm, so it won't change very often and when it will, there will be pretty good reason for it... I guess :)
    – Kovas
    Commented Jun 15, 2011 at 8:32

Hook into the action 'save_post' (example). You get the post ID as a parameter, and then you can do everything you did before. With one save action. :)

  • can you please look at my answer and see if this is the best way. Because I thought about wp_update_post but that too calls save_post. And I am not sure if there is any function to set the post title. Commented Jun 13, 2011 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.