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 am using custom posts, and in these, I don't a need for the title.

This causes Wordpress to set the titles of my posts to "Auto Draft".

I'd like to change the title's value to something else, computed from other fields in my post.

How do I go about doing that using save_post or some other means?

share|improve this question
    
Please edit your question to include your register_post_type() call. –  Chip Bennett Nov 21 '12 at 18:54
    
What are you trying to achieve exactly? You don't want a post title for your CPT at all or you want it to be set from a custom field value? –  Rutwick Gangurde Nov 21 '12 at 19:01
1  
I don't want it at all, but in the posts list I can't remove it as I won't be able to edit posts anymore. This means that I need a way to place a "fake" title of some kind instead. –  Tsahi Levent-Levi Nov 21 '12 at 19:28
    
So, what you really want to do is to modify the manage posts screen, to output different columns for your custom post type? If so, that might be a more beneficial question to ask. :) –  Chip Bennett Nov 21 '12 at 19:38
add comment

2 Answers 2

up vote 5 down vote accepted

I had the exact same need, so I wrote this function - which works. Modify it to your needs. Hope this helps.

// set daily rating title
function set_rating_title ($post_id) {
    if ( $post_id == null || empty($_POST) )
        return;

    if ( !isset( $_POST['post_type'] ) || $_POST['post_type']!='rating' )  
        return; 

    if ( wp_is_post_revision( $post_id ) )
        $post_id = wp_is_post_revision( $post_id );

    global $post;  
    if ( empty( $post ) )
        $post = get_post($post_id);

    if ($_POST['rating_date']!='') {
        global $wpdb;
        $date = date('l, d.m.Y', strtotime($_POST['rating_date']));
        $title = 'TV ratings for ' . $date;
        $where = array( 'ID' => $post_id );
        $wpdb->update( $wpdb->posts, array( 'post_title' => $title ), $where );
    }
}
add_action('save_post', 'set_rating_title', 12 );
share|improve this answer
add comment

Try the filter 'default_title':

add_filter( 'default_title', 'my_default_title', 10, 2 );

function my_default_title( $post_title, $post ){

  $custom_post_type = 'my_awesome_cpt';

  // do it only on your custom post type(s)
  if( $post->post_type !== $custom_post_type )
    return $post_title;

  // create your preferred title here
  $post_title = $custom_post_type . date( 'Y-m-d :: H:i:s', time() );

  return $post_title;
}
share|improve this answer
    
This solution does not allow to create a title "computed from other fields in my post" - as requested. It is fine, if you can generate an automatic title for all posts of that kind. But if you need to depend on some dynamic variable then this won't help. –  Biranit Goren Nov 21 '12 at 21:45
    
@Biranit Goren What does "computed from other fields in my post" mean? Which field did you miss in the post object stored in $post? Please read the initial question and the comments below. An automatic generated post titel is not requested. Only a fake post titel ( a replacement for WordPress "Auto Draft" ) is needed. –  Ralf912 Nov 22 '12 at 20:10
add comment

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.