1

I have a meta_key called "_ordre" and each time I create a new custom post, a new meta_value is set for that meta_key.

The thing is, when I set the value of '_ordre' to 0, the meta_value is not saved into the database. Anything else is ok but 0.

here is my code:

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 );

Any help would be appreciated. Thank you.

EDIT: var_dump($_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 Answer 1

0

if ( !$value ) evaluates to TRUE when the value is 0.

Alternative:

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

update_post_meta( $post_id, '_ordre', $_POST['_ordre'] );

You don’t need add_post_meta(), the call to update_post_meta() will do that automatically.

7
  • Thank you toscho. Just replaced everything under the last "return" of my code with yours. Now, whatever the meta_value I enter, it's not saved at all.
    – Baylock
    Commented Feb 3, 2013 at 13:27
  • 1
    Could you please add the result of var_dump( $_POST ); to your question?
    – fuxia
    Commented Feb 3, 2013 at 13:32
  • The var_dump has been added to the initial question. Thank you.
    – Baylock
    Commented Feb 3, 2013 at 13:57
  • $_POST is never a string, always an array. Please add use exactly the line I suggested in my previous comment.
    – fuxia
    Commented Feb 3, 2013 at 13:59
  • Ok, sorry. It's done.
    – Baylock
    Commented Feb 3, 2013 at 14:12

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.