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'm working with custom post types to create a custom "product review" type. I'm customizing the UI to have fields for the "Reviewer", "Product", and their "Review". This doesn't really require a post title though, so I've removed that field.

This is all working fine, but when I go to look at all Product Reveiws, the titles are completely unhelpful (Auto Draft, Auto Draft 2, etc.). What I want to do is automatically set the post title to be a combination of the Reviewer's name and the product they reviewed; something along the lines of "John Smith, Car Radio".

I have a function that hooks into the save_post action, and updates the custom meta fields I have set. I figure I need to add something here to accomplish what I'm trying to do, but I'm not sure what function or process this requires.

Thanks in advance!

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Jeremy,

Excellent work not just leaving it at "Auto Draft". It's tricky when these types of CPTs don't have titles. Here is some code I've used to accomplish a similar task.

You'll need to adjust this for your situation, but it shows you a way to get this done. In particular, you can use the wp_insert_post_data filter to change the title before you add it to the database. Now, one of the biggest mistakes you can make here is filtering ALL post titles. If you are not careful to test for the right context (e.g., when you are saving and/or editing your "product review" CPT, you will find that ALL titles in your site get mangled. My recommendation is to make use of nonce fields in your meta boxes to detect when the right form is submitted.

Here's my code:

add_filter('wp_insert_post_data', 'change_title', 99, 2);

function change_title($data, $postarr)
{    
    // If it is our form has not been submitted, so we dont want to do anything
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // Verify this came from the our screen and with proper authorization because save_post can be triggered at other times
    if(!isset($_POST['my_nonce_field'])) return;

    // If nonce is set, verify it
    if(isset($_POST['my_nonce_field']) && !wp_verify_nonce($_POST['my_nonce_field'], plugins_url(__FILE__))) return;

    // Get the associated term name
    foreach($_POST['tax_input']['complaint-type'] as $term) {$term_id = $term;}

    // Get name of term
    $term = get_term_by('id', $term_id, 'complaint-type');

    // Combine address with term
    $title = $_POST['address']['address1'].' ('.$term->name.')';
    $data['post_title'] = $title;

    return $data;
}

Don't get caught up in my manipulations of the title. Just realize that you need to set $data['post_title'] and return $data.

share|improve this answer
    
Thanks! I was able to get the basic bits of this working. I don't know how to set up nonce fields to work in this, so I just used a conditional check on $data['post_type']. Once I figure out nonce fields, I'll add that too (I know it's better security). One thing I did notice is that in my $data['post_type'] check, when I just called "return;", it gave an error - I needed to "return $data;" instead. Not sure why that is, but figured I'd note it here if anybody comes across this in the future. –  Jeremy Aug 22 '11 at 23:38
    
Read this article: markjaquith.wordpress.com/2006/06/02/wordpress-203-nonces. You must learn about nonces for good security! Also, your check for post type may not work in all situations. You'll be surprised that the wp_insert_post_data is executed in strange places. It would even be better if you had a hidden field in your meta box that you detect. –  tollmanz Aug 22 '11 at 23:58
    
Cool, I'll use the hidden field check for now then. I read that article recently, and I get the idea of nonces; just not sure how to incorporate them in practice. I'll definitely get it figured out though (and come back for help when needed)! Thanks again! –  Jeremy Aug 23 '11 at 0:10
1  
i find that if i simply return; i produce warnings about array_keys() expecting an array but getting a string when one of the checks fails. Warning: array_keys() expects parameter 1 to be array, string given in wp-includes\wp-db.php on line 1207 Warning: Invalid argument supplied for foreach() in wp-includes\wp-db.php on line 1209 Warning: implode() [function.implode]: Invalid arguments passed inwp-includes\wp-db.php on line 1218 –  helgatheviking Mar 11 '12 at 23:13
    
holy batman, after HOURS looking at that... apparently it is only on 'Add new post' when this filter gets run but there is no $_POST. i found that if($_POST && !isset($_POST['_document_meta_nonce'])) return; gets rid of the warnings. same if($_POST) before the verify nonce too. –  helgatheviking Mar 11 '12 at 23:24

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.