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've removed "Title" from a custom post type that I created. Which makes it that all new posts gets the name "Auto Draft".

To get around this I want to be able to fetch a value/post name from a meta box and save that as the new title. Is this possible?

This is what I'm using, and the value of this meta should be the new title

 function save_title_meta($post_id, $post) {

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['meta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}

// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
    return $post->ID;

// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.

$project_meta['_title'] = $_POST['_title'];

// Add values of $project_meta as custom fields

foreach ($project_meta as $key => $value) { // Cycle through the $project_meta array!
    if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
        update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn't have a value
        add_post_meta($post->ID, $key, $value);

    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}

 }

add_action('save_post', 'save_title_meta', 1, 2); // save the custom fields
?>




echo '<input type="hidden" name="meta_noncename" id="meta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

// Get the location data if its already been entered
$title = get_post_meta($post->ID, '_title', true);

// Echo out the field
echo '<input type="text" name="_title" value="' . $title  . '" class="widefat" />';
?>
share|improve this question

1 Answer 1

up vote 6 down vote accepted

Even though you've disabled title for your custom post type, if you use the post name post_title for your own title input, WordPress will still use it and update the post title accordingly.

share|improve this answer
    
Thanks you for your reply (again :)) Could you be more specific on which lines to change? As you might have realized, I'm still kind of new to this. –  INT Mar 15 '11 at 0:02
    
Just the last line - change name="_title" to name="post_title" - then you can remove any code that is manually setting the title, as WordPress will pick it up from $_POST! –  TheDeadMedic Mar 15 '11 at 1:07
    
Thanks alot. Works! –  INT Mar 15 '11 at 9:53
1  
For the record, the technique works the same with "hidden" fields, as long as the name is the same. I discovered this working with the WPAlchemy class. Thanks for the tip! –  Jonathan Wold Mar 1 '12 at 23:17

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.