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 few custom post types set up. However, a couple of them have the title field removed from the edit screen because a title does not make sense in some aspects. However, I now find that all posts are being saved improperly with the text "Autosaved Post" or whatever.

Is it possible to set the post title upon saving the post to be the assigned category name? The reason I want this is because I am building a record label website with the following custom post types:

  • Artists
  • Homepage Carousel
  • Videos
  • Tours

The homepage carousel does not need a title and I would like it to perhaps be the following format: "Bandname Carousel" bandname being the category name of which would only be band names.

Should I add the title field back in and use jQuery to perhaps set the title instead of writing a custom Wordpress plugin and set the field to read only so the user can't make changes to it?

The reason I want to set the title is to stop the annoying "Autosaved Post" value being added to the title hence preventing the user from being able to sort through and differentiate between posts that have been added.

share|improve this question
    
Do you ever only have one category per post? Do you really want lots of posts with the same name? –  MikeSchinkel Jan 6 '11 at 8:49
    
Hi Mike, the plan is that the posts that are going to be assigning the category name as the title are always only going to have one category assigned, so duplicate titles is not an issue I guess you could say. I was thinking that the category named with a slug on the end of the title would prevent duplicates in other post types as well. –  DigitalSea Jan 6 '11 at 12:20
add comment

1 Answer

up vote 1 down vote accepted

Q: Is it possible to set the post title upon saving the post to be the assigned category name?

A: Yes it is.

Each time a post is saved or created, a filter is called. You can hook into that filter and set the title of the post to the value you want. This will, in contrast to a solution with jQuery, ensure that your data is correct regardless how the editor configured his browser or if something is broken on his system.

If you want to find out more about the hooks I'm talking about, these two functions are a good place to start: wp_update_post() and wp_insert_post(). The related source-code is available in /wp-includes/post.php. You can find all associated hooks inside the functions code.

There are multiple available, so I can only suggest one at best guess. That would be wp_insert_post_data in line 2 531, you can change/set the title by hooking into that filter:

add_filter('wp_insert_post_data', function($data, $postarr) {
    /* your job */
    $data['post_title'] = $defaultTitle;
    $return $data;
}, 10, 2);

Or it's possible even better to hook into save_post action in line 2 620 as it's already saved within the database and might work for both functions. The principle is the same: You modify the data on your own.

share|improve this answer
    
Exactly what I was after. I don't know why I didn't think of using the save_post hook myself. I've written a couple of Wordpress plugins and know filters and hooks quite well. Still can't believe I didn't think of this. Thanks for your help Hakre. –  DigitalSea Jan 7 '11 at 2:18
    
Even I know some hooks already, I always go into the source-code again and look which are in there and when they get invoked. That often helps to find the best ones. Because you can't know all ;) –  hakre Jan 8 '11 at 19:57
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.