自动将标题另存为自定义元字段值

时间:2012-05-26 作者:dev-jim

我创建了一个自定义的帖子类型,在这里我删除了“标题”,相反,我想使用我的一个自定义元值来成为标题。这只是为了确保标题看起来不错。

知道即使我删除了“title”部分,post name post\\u title仍然存在,所以在我的函数中,我添加了基于自定义元值保存标题的代码。

代码运行良好,这是我第一次只保存帖子。然而,当我更新帖子时,标题消失了。

我错过了什么或做错了什么?

谢谢

更新2这是meta box和save\\u post filter功能的完整代码:

    // 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
嗯?

1 个回复
SO网友:Tommixoft

因为您的代码似乎有缺陷/不完整,所以我编写了我的代码。这将在post中添加一个带有一个文本字段的元框(您可以将其更改为添加到自定义post\\u类型)-post title。我还必须使用QUERY来更新/添加标题,因为我找不到其他方法,$post->post_title = \'some title\' 不要工作。

如果您对此代码满意-请标记为良好答案,祝您好运:)

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

结束