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 create a custom post type where I removed the "Title", instead I want to use one of my custom meta value to become the title. This is just to make sure the title look good.

Know that the post name post_title still exist even I remove the "Title" section, so in my function I added the code to save the title base on the custom meta value.

The code working fine for the first time I save the post only. However, when I update the post, the title disappeared.

What did I missed or done wrong?

Thanks

Update 2 This is the entire codes for meta box and save_post filter function:

    // Add meta box to editor
function add_listing_box() {
    global $meta_box; // get all of the options from the $meta_box array

    add_meta_box($meta_box['id'], $meta_box['title'], 'box_output', $meta_box['page'], $meta_box['context'], $meta_box['priority']);

}
add_action('admin_menu', 'add_listing_box');


function box_output () {
    global $meta_box, $post;
    echo '<input type="hidden" name="listing_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
    echo '<table class="form-table">';

    foreach ($meta_box['fields'] as $field) { // do this for each array inside of the fields array
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);

        echo '<tr>', // create a table row for each option
                '<th style="width:25%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>:</td><td>';
        switch ($field['type']) {

            case 'text': // the HTML to display for type=text options
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:30%" />', '', $field['desc'];
                break;     

            case 'textarea': // the HTML to display for type=textarea options
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="8" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '', $field['desc'];
                break;

            case 'select': // the HTML to display for type=select options
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>',$field['desc'];
                break;

            case 'radio': // the HTML to display for type=radio options
                foreach ($field['options'] as $option) {
                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                }
                break;

            case 'checkbox': // the HTML to display for type=checkbox options
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                break;

            case 'image':  
               $image = get_template_directory_uri().'/images/image.png';  
               echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';  
              if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }  
              echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> 
                <img src="'.$image.'" class="custom_preview_image" alt="" /><br /> 
                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                    <br clear="all" /><span class="description">'.$field['desc'].'';  
                 break;      
        }
        echo     '<td>',
            '</tr>';
    }

    echo '</table>';
     $title1 = get_post_meta($post->ID, 'cmf', true); 
     echo '<input type="hidden" name="post_title" id="post_title" value="',$title1,'" size="30" style="width:30%" />';
         //here where I try to get the custom title.

}

// Save data from meta box
function for_meta_save_data($post_id,$post) {
    global $meta_box;

    // verify nonce -- checks that the user has access
    if (!wp_verify_nonce($_POST['listing_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }


    foreach ($meta_box['fields'] as $field) { // save each option
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
        $title1 = $_POST["post_title"];
        if ($new && $new != $old) { // compare changes to existing values
            update_post_meta($post_id, $field['id'], $new);

        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);

        }
    }
    $post->post_title = $_POST["post_title"];//here is where i try to  save post title.

}
add_action('save_post', 'for_meta_save_data'); // save the data

Well?

share|improve this question
    
I use this: add_action('save_post', 'YOUR_FUNCTION', 1, 2); // save the custom fields i don't remember why i use 1,2 but you may try this too :) –  Tommixoft May 27 '12 at 12:14
    
Well, I don't know if i get your idea. I added $title1 = get_post_meta($post->ID, 'cmf1', true); echo '<input type="text" name="post_title" id="post_title" value="',$title1,'" size="30" style="width:30%" />'; to my meta box and added $post->post_title = $_POST["post_title"]; to save_post filter. It works, but i need to save twice to get the new title saved. Sorry if I got you wrong. –  dev-jim May 27 '12 at 13:10
    
@Tommix, thanks for your advise, I manage to find another source that solved my problem [link]wordpress.stackexchange.com/questions/51871/…. Btw, still appreciate your helps. –  dev-jim May 27 '12 at 15:42
    
:D strange what you find is just adds text to TITLE field, text which is cleared once you select the field, but in your case it worked because you hiden the field :D –  Tommixoft May 27 '12 at 16:15
    
Yes, you are right, I also notice the problem now. The permalink remains as ?listing=auto-draft which mean the title not saved in the db. :( –  dev-jim May 27 '12 at 16:30

1 Answer 1

Well because your code seems like to be bugy/not full - i wrote my code. This will add one meta box with one text field in post (you can change it to be added in your custom post_type)- Post title. Also i had to use QUERY to update/add title, because i can not find any other way, $post->post_title = 'some title' do not work.

If you happy with this code - please mark as good answer, good luck :)

EDIT:

 // Save post with your title
function save_the_title($post_id) {
global $wpdb;


if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
  return;

if ( !current_user_can( 'edit_post', $post_id ))
    return $post_id;

 $newtitle = '';  // SET YOUR TITLE HERE 
 $old_title = get_post_meta($post_id, '_title', true);
 if($newtitle != '' && $newtitle != $old_title) {
 update_post_meta($post_id, '_title', $newtitle);
 $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d ",$title1,$post_id) );

} 

 }

 add_action('save_post', 'save_the_title'); // save the custom fields
share|improve this answer
    
well, thanks for your effort. But if I not mistaken, your codes need me to insert the title by my own? If so, what is the different with the default title? My intention is to insert the post title with the custom meta field. –  dev-jim May 27 '12 at 19:04
    
but somebody have to set the custom meta field value yes? so what's the diference? i don't understand than what you want :) at what point where is title made? who will create title? you dynamicly? via update_post_meta ? if yes than you can easily change this code. to like 5 lines that will work for you :) You use $_POST so this means someone posted title in some field, that's why i made example with meta box :) –  Tommixoft May 27 '12 at 19:19
    
I think I got your point, and I tried the codes. Strangely, I have to save twice to see the changes. Which mean, if I change the custom field and save it, the title doesn't change, but if I save it again with the same value, then the title changed. Did you experienced the same problem? –  dev-jim May 27 '12 at 19:29
    
updated the code. So now you have to set the title in $newtitle with your desired title name. –  Tommixoft May 27 '12 at 19:41
    
The new code return me to the 'No title', even I insert the new value in the custom field. There is another problem I discovered, the permalink is not response to the changes of the title. It just show auto-draft http://example.com/post-type?=auto-draft . :( –  dev-jim May 27 '12 at 20:26

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.