0

I have checkbox in my post and I want to delete a database row when checkbox is not checked but when I press update button nothing happend and the row wasn't deleted. here is my codes

if (isset($_POST['save-author-to-book'])) {
        $key = 'save-author-to-book';
        $values_to_save = array();
        $new_values = $_POST["save-author-to-book"];
        $existing_values = get_post_meta( $post_id, $key, true ) ;



        if(!empty($new_values)){
            foreach($new_values as $new_value ){
            $values_to_save[] = $new_value ;


            }
         update_post_meta( $post_id, $key, $values_to_save ); 
        }


    }
    else{

        delete_post_meta( $post_id, $key, $meta_value=''); 
        // break;

    }

any idea would be appreciated.

0

This is because the value of meta key is inside the if clause and is not set for the else.

try

else {
        $key = 'save-author-to-book';
        delete_post_meta( $post_id, $key, $meta_value=''); 
        // break;

    }
  • Yeah it was... happens to all of us ;) – Maruti Mohanty Aug 24 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.