I'm using RW_Meta_Box plugin by Rilwis.

I'm using a CPT and added a custom meta-box and removed the title and editor.

But I still want to set the title for some obvious reasons.

I created the CPT and meta-box and everything worked perfectly including the all posts page using custom column hooks.

As soon as I sat to set post title to one of the fields in the meta-box, I failed to get the desired results.I tried to solutions I picked by searching through this Q&A site.

Initially I tried:


add_action('submitpost_box', 'hidden_type_title');

function hidden_type_title() {
    global $current_user, $post, $post_type;
    global $prefix;
    $md = rwmb_meta($prefix . 'name', array('type' => 'text'), $post->ID);
    if ($post->post_type == 'MY_CPT_NAME') {
    <input type="hidden" name="post_title" value="<?php echo esc_attr(htmlspecialchars($md)); ?>" id="title" />
    } else {

The above code works fine but it has a problem I am unable to detect. which is, I need to update the post twice to set the post title.

Therefore I went on to try the save_post hook like so:


add_action('save_post', 'post_updated');

function post_updated($post_id) {
    global $current_user, $post;
    if ($post->post_type != 'MY_CPT_NAME') {

    global $prefix;
    $md = rwmb_meta($prefix . 'name', array('type' => 'text'), $post_id);
    // verify post is not a revision & not an autosave
    if (!wp_is_post_revision($post_id) && !(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) {
    // set the new post title
    $post->ID = $post_id;
    $post->post_title = $md;

    // update the post, removing the action to prevent an infinite loop
    remove_action('save_post', 'post_updated');
    add_action('save_post', 'post_updated');

Now, I'm in a worse situation, the post title is instantly set but my meta-box data is somehow not being saved.

What might be wrong with the first solution?

  • On the first block of code : echo esc_attr(htmlspecialchars($md)); won't have a value in your hidden input until you save... hence needing to save twice to set the title. What is the name of the field you want to base the post title off of? Jul 16, 2013 at 13:48
  • @helgatheviking No I think, $md has value after submtting the form. just checkecd. Jul 16, 2013 at 14:29
  • yes, that's what you said in your question: "I need to update the post twice to set the post title." On a new post this hidden field isn't populated until the post is saved the first time, requiring you to save a second time to trigger the post title. Jul 16, 2013 at 14:34

1 Answer 1


Here's a solution that take's advantage of Rilwis' action hook... so you can profit from his nonce checking without needing to add your own.

add_action('rwmb_after_save_post', 'post_updated');

function post_updated($post_id) {

    // verify post is not a revision & not an autosave
    if (!wp_is_post_revision($post_id) && !(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)) {

    global $prefix;
    $prefix . 'name';

    // check that the custom field is being POSTED 

    if( isset( $_POST[$prefix . 'name'] ) ){    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $my_post['post_title'] = sanitize_title( $_POST[$prefix . 'name'] );

        // Update the post into the database
        wp_update_post( $my_post );   



To limit this function to only run for a specific metabox, you could use this add_action instead:

add_action("rwmb_{$meta_box['id']}_after_save_post", 'post_updated');

Without seeing your metabox definitions, I don't know what to change the ID to, so you will have to do that.

NB: Untested, but I think it is close. Please see wp_update_post() in the codex for how that function works.


Your Answer

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

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