Question

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) ...
    <?php
} 

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['book']);
    }
    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?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top