自定义发布类型元数据不保存

时间:2012-01-25 作者:Anders Kitson

我有以下代码。我正在尝试将数据输入自定义元数据库,然后保存它。当我去查看时,它已经进入了一个帖子,但是name列中没有任何内容。当我点击save时,我也得到了一个帖子。带有空白页面的php页面,这是我需要在我的主题中创建的页面,并将其返回到当前页面或其他页面。

<?
/**
*   Custom post-type booktime
**/

add_action(\'init\', \'booktime_register\');

function booktime_register() {

    $labels = array(
        \'name\' => _x(\'booktime\', \'post type general name\'),
        \'singular_name\' => _x(\'booktime\', \'post type singular name\'),
        \'add_new\' => _x(\'add new\', \'add new\'),
        \'add_new_item\' => __(\'add new item\'),
        \'edit_item\' => __(\'edit item\'),
        \'new_item\' => __(\'new item\'),
        \'view_item\' => __(\'view item\'),
        \'search_items\' => __(\'search items\'),
        \'not_found\' =>  __(\'nothing here\'),
        \'not_found_in_trash\' => __(\'not found in trash\'),
        \'parent_item_colon\' => \'\'
    );

    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => null,
        \'supports\' => array(\'title\',\'editor\',\'thumbnail\',\'comments\',\'custom-fields\',\'revisions\')
      );

    register_post_type( \'booktime\' , $args );
}

?>

<?php

add_action("manage_posts_custom_column",  "booktime_custom_columns");
add_filter("manage_edit-booktime_columns", "booktime_edit_columns");

function booktime_custom_columns($column){
    global $post;

    switch ($column) {
        case "name":
            the_excerpt();
        break;
        case "address":
            $custom = get_post_custom();
            echo $custom["address"][0];
        break;
        case "phone":
            $custom = get_post_custom();
            echo $custom["phone"][0];
        break;
        case "date":
            $custom = get_post_custom();
            echo $custom["date"][0];
        break;
        case "bottles":
            $custom = get_post_custom();
            echo $custom["bottles"][0];
        break;

    }
}

function booktime_edit_columns($columns){
    $columns = array(
        "cb" => "<input type=\\"checkbox\\" />",
        "title" => "Post should show",
        "description" => "name",
        "address" => "address",
        "phone" => "phone",
        "date" => "date",
        "bottles" => "bottles"
    );

    return $columns;
}

?>

<?php
add_action("admin_init", "admin_init_booktime");

function admin_init_booktime(){

    add_meta_box("name", "name", "name", "booktime", "normal", "low");

}
?>

<?php
function name() {
    global $post;
    $custom = get_post_custom($post->ID);
    $name = ( !empty($custom["name"][0]) ) ? $custom["name"][0]: "";
    ?>
    <p><label>Name:</label><br />
        <input type="text" name="name" value="<?php echo $name; ?>"/></p>
    <?php
}
?>

<?php
add_action(\'save_post\', \'save_details_booktime\');

function save_details_booktime(){
    global $post;
    $custom_meta_fields = array( \'name\' );

    foreach( $custom_meta_fields as $custom_meta_field ):
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""):
            update_post_meta($post->ID, $custom_meta_field, $_POST[$custom_meta_field]);
        endif;
    endforeach;

}
?>

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

您需要从替换字段名name 从那以后name 是一个保留字,可能是导致空白页的原因,更好的做法是在字段名前加前缀。

现在,在save\\u details\\u booktime函数中,请确保您不是正确的帖子类型,并且没有处于自动保存状态,如下所示:

 function save_details_booktime($post_id ) {
    global $post;   
    //skip auto save
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }
    /check for you post type only
    if( $post->post_type != "booktime" ) {
        return;

    }
    $custom_meta_fields = array( \'name\' ); //change from name to something else

    foreach( $custom_meta_fields as $custom_meta_field ){
        if(isset($_POST[$custom_meta_field]) && $_POST[$custom_meta_field] != ""){
            update_post_meta($post_id, $custom_meta_field, $_POST[$custom_meta_field]);
        }
    }
}

SO网友:Jared

这个save_post 操作将返回您为您保存的帖子的ID。

您可能还需要验证用户更新帖子的权限,并确保未运行Autosave之类的任务。我在下面添加了这些。

(另请阅读Bainternet的回答)

add_action(\'save_post\', \'save_details_booktime\');

function save_details_booktime( $id ){

    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
        return;

    // Check permissions
    if ( \'page\' == $_POST[\'post_type\'] ) {
        if ( !current_user_can( \'edit_page\', $id ) )
            return;
    } else {
        if ( !current_user_can( \'edit_post\', $id ) )
            return;
    }

    $custom_meta_fields = array( \'some_name\' );

    foreach( $custom_meta_fields as $custom_meta_field ):
        // Grab the existing meta data (will return an empty string if none)
        $meta_exists = get_post_meta( $id, $custom_meta_field, true );

        // If the meta doesn\'t match what was $_POST\'ed
        if( $_POST[$custom_meta_field] != $meta_exists && $_POST[$custom_meta_field] != \'\' )
            update_post_meta( $id, $custom_meta_field, $_POST[$custom_meta_field] );

        // If the meta does exist, but the new data is blank, delete it
        if( $meta_exists != \'\' && $_POST[$custom_meta_field] == \'\' )
            delete_post_meta( $id, $custom_meta_field );

    endforeach;

}
请务必阅读get_post_meta, update_post_meta, delete_post_meta, 和save_post 有关上述功能的更多信息。

结束