Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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).

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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' );
share|improve this answer
    
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 Jun 14 '11 at 21:08
    
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. –  Hameedullah Khan Jun 15 '11 at 5:39
    
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 Jun 15 '11 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. :)

share|improve this answer
    
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. –  Hameedullah Khan Jun 13 '11 at 17:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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