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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *