6

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" />';
?>
1

1 Answer 1

9

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.

3
  • 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, 2011 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! Mar 15, 2011 at 1:07
  • 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! Mar 1, 2012 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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