I have some text fields in a custom post type that I fill in at first, but need to delete on a future update of the post. When I delete the text in the field and save the post, the text I deleted comes back! If I enter a space, it saves OK. Does anybody know how I can fix this?

Here are the parts of the code for my custom post type that display and save the field data (didn't post the custom post type init code because it's right from the codex):

function book_info()
    global $post;
    $custom = get_post_custom($post->ID);
    $book_title = $custom["book_title"][0];
    ... (more field variables) ...
    <p><label>Book Title:</label><br />
    <input class="medium" name="book_title" value="<?php echo esc_html($book_title); ?>" /></p>
    ... (more field display blocks) ...

add_action('save_post', 'save_book');
function save_book($post_id)
    // Verify this came from our screen with proper authorization.
    if ( !wp_verify_nonce( $_POST['book_info_noncename'], 'book_info'.$post_id )) {
        return $post_id;

    // Determine whether this is an autosave routine. If so, our form has not been submitted and we don't want to do anything.
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

    // Check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    // Now that authentication is complete, find and save the data.
    $post = get_post($post_id);
        if ($post->post_type == 'book') { 

            if (isset($_POST["book_title"]) && $_POST["book_title"] <> '') update_post_meta($post->ID, "book_title", $_POST["book_title"]);
            ... (more field save statements) ...

    return $post_id;

If I enter text in the "book_title" field and save, it works. If I then delete that text and save, the text is still there when the edit post screen refreshes after clicking save. If I delete that text and replace it with a space character, it saves. I need to be able to delete the text from the field without having to replace it with a space character. Any thoughts on how to accomplish this?

1 Answer 1


The problematic line is this one:

if (isset($_POST["book_title"]) && $_POST["book_title"] <> '') update_post_meta($post->ID, "book_title", $_POST["book_title"]);

That logic says: If the submitted form contains a field called 'book_title' and if the content of the 'book_title' field does not equal an empty string, then save it. In your situation, the first condition should be met (HTML input fields with a 'text' type will appear in the $_POST global even when they're empty), but the second one is failing. Change it to:

if ( isset( $_POST["book_title"] ) ) 
    update_post_meta($post->ID, "book_title", $_POST["book_title"]);
  • Your solution works, and because you explained why, I also understand it. Thank you! I found that save method in a custom post type tutorial somewhere. Any idea why that code would be there in the first place?
    – Joe
    Apr 15, 2012 at 23:28
  • Glad to help! I assume that the code was originally there in order to prevent you from accidentally deleting an option. Apr 16, 2012 at 12:14

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.