Saving zero as meta value

时间:2013-02-03 作者:Baylock

我有一个名为“\\u ordre”的meta\\u键,每次创建新的自定义帖子时,都会为该meta\\u键设置一个新的meta\\u值。

问题是,当我将“\\u ordre”的值设置为0时,meta\\u值不会保存到数据库中。除了0,其他都可以。

这是我的代码:

function wp_audio_save_meta( $post_id, $post ) 
{
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;
    if ( !isset( $_POST[\'wp_audio_posts_nonce\'] ) )
        return;
    if ( !wp_verify_nonce( $_POST[\'wp_audio_posts_nonce\'], plugin_basename( __FILE__ ) ) )
        return;
    if ( !current_user_can( \'edit_post\', $post->ID ) )
        return;
    if ( $post->post_type == \'revision\' ) 
        return;

    $value = $_POST[\'_ordre\'];

    if ( get_post_meta( $post->ID, \'_ordre\', FALSE ) ) 
    {
        update_post_meta( $post->ID, \'_ordre\', $value );
    } 
    else 
    { 
        add_post_meta( $post->ID, \'_ordre\', $value );
    };

    if ( !$value ) delete_post_meta( $post->ID, \'_ordre\' );

}
add_action( \'save_post\', \'wp_audio_save_meta\', 1, 2 );
任何帮助都将不胜感激。非常感谢。

编辑:var\\u dump($\\u POST):

array(56) 
{ 
["_wpnonce"]=> string(10) "f874332a30" 
["_wp_http_referer"]=> string(44) "/admin/wp-admin/post-new.php?post_type=audio" 
["user_ID"]=> string(1) "2" 
["action"]=> string(8) "editpost" 
["originalaction"]=> string(8) "editpost" 
["post_author"]=> int(2) 
["post_type"]=> string(5) "audio" 
["original_post_status"]=> string(10) "auto-draft" 
["referredby"]=> string(40) "http://www.xxx.com/admin/wp-admin/"  
["_wp_original_http_referer"]=> string(40) "http://www.xxx.com/admin/wp-admin/"   
["auto_draft"]=> string(1) "1" 
["post_ID"]=> string(4) "1539" 
["autosavenonce"]=> string(10) "f9cf5bed77" 
["meta-box-order-nonce"]=> string(10) "629f95f577" 
["closedpostboxesnonce"]=> string(10) "b121fed9eb" 
["post_title"]=> string(0) "" 
["samplepermalinknonce"]=> string(10) "96f452a929" 
["post_category"]=> array(1) { [0]=> string(1) "0" } 
["newcategory"]=> string(29) "New category name" 
["newcategory_parent"]=> string(2) "-1" 
["_ajax_nonce-add-category"]=> string(10) "411da663fa" 
["wp_custom_audio_file_noncename"]=> string(10) "584d7b259e" 
["xxx"]=> string(4) "1539" 
["title_up_1"]=> string(0) "" 
["wp_audio_posts_nonce"]=> string(10) "584d7b259e" 
["_ordre"]=> string(1) "0" 
["post_name"]=> string(0) "" 
["wp-preview"]=> string(0) "" 
["hidden_post_status"]=> string(5) "draft" 
["post_status"]=> string(7) "publish" 
["hidden_post_password"]=> string(0) "" 
["hidden_post_visibility"]=> string(6) "public" 
["visibility"]=> string(6) "public" 
["post_password"]=> string(0) "" 
["jj"]=> string(2) "03" 
["mm"]=> string(2) "02" 
["aa"]=> string(4) "2013" 
["hh"]=> string(2) "15" 
["mn"]=> string(2) "03" 
["ss"]=> string(2) "28" 
["hidden_mm"]=> string(2) "02" 
["cur_mm"]=> string(2) "02" 
["hidden_jj"]=> string(2) "03" 
["cur_jj"]=> string(2) "03" 
["hidden_aa"]=> string(4) "2013" 
["cur_aa"]=> string(4) "2013" 
["hidden_hh"]=> string(2) "15" 
["cur_hh"]=> string(2) "15" 
["hidden_mn"]=> string(2) "03" 
["cur_mn"]=> string(2) "03" 
["original_publish"]=> string(7) "Publish" 
["publish"]=> string(7) "Publish" 
["post_mime_type"]=> string(0) "" 
["ID"]=> int(1539) 
["comment_status"]=> string(6) "closed" 
["ping_status"]=> string(6) "closed" 
}

1 个回复
SO网友:fuxia

if ( !$value ) 计算结果为TRUE 当值为0.

备选方案:

if ( ! isset ( $_POST[\'_ordre\'] ) ) 
    return delete_post_meta( $post_id, \'_ordre\' );

update_post_meta( $post_id, \'_ordre\', $_POST[\'_ordre\'] );
你不需要add_post_meta(), 呼叫update_post_meta() 将自动执行此操作。

结束