Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm having an issue with custom functions at the moment, once a value has been added to the database I can not delete the value, what's the best way to solve this?

Basically I've created a custom post type for a games database, when adding a game there are several custom post functions; game developer, publisher, review score and a review URL, now if I enter a test URL it will save that but when I try to delete it, it still appears and wont save the deleted value.

Here is the code I'm using:

<?php
function display_games_database_meta_box( $games_database ) {
// Retrieve information from the game
$game_publisher = esc_html( get_post_meta( $games_database->ID, 'game_publisher', true     ) );
$game_developer = esc_html( get_post_meta( $games_database->ID, 'game_developer', true ) );
$game_rating = intval( get_post_meta( $games_database->ID, 'game_rating', true ) );
$game_review_link = esc_html( get_post_meta( $games_database->ID, 'game_review_link', true ) );
$game_release = esc_html( get_post_meta( $games_database->ID, 'game_release', true ) );
?>
<table>
    <tr>
        <td style="width: 100%">Game Publisher</td>
        <td><input type="text" size="80" name="games_database_game_publisher" value="<?    php echo $game_publisher; ?>" /></td>
    </tr>
    <tr>
        <td style="width: 100%">Game Developer</td>
        <td><input type="text" size="80" name="games_database_game_developer" value="<?php echo $game_developer; ?>" /></td>
    </tr>
    <tr>
        <td style="width: 150px">Game Rating</td>
        <td>
            <select style="width: 100px" name="games_database_rating">
            <option value="No Score">No Score</option>
            <?php
            // Generate all items of drop-down list
            for ( $rating = 10; $rating >= 1; $rating -- ) {
            ?>
                <option value="<?php echo $rating; ?>" <?php echo selected( $rating, $game_rating ); ?>>
                <?php echo $rating; ?>/10<?php } ?>

            </select>
        </td>
    </tr>
    <tr>
        <td style="width: 100%">Review Link (Use the full URL)</td>
        <td><input type="text" size="80" name="games_database_review_link" value="<?php echo $game_review_link; ?>" /></td>
    </tr>        
    <tr>
        <td style="width: 100%">Game Release Date (Format: June 26th 2013)</td>
        <td><input type="text" size="80" name="games_database_game_release" value="<?    php echo $game_release; ?>" /></td>
    </tr>
</table>
<?php
}
?>
<?php
// Adds and saves the game to the database
add_action( 'save_post', 'add_games_database_fields', 10, 4 );
function add_games_database_fields( $games_database_id, $games_database ) {
// Check post type for games
if ( $games_database->post_type == 'games_database' ) {
    // Store data in post meta table if present in post data
    if ( isset( $_POST['games_database_game_publisher'] ) && $_POST['games_database_game_publisher'] != '' ) {
        update_post_meta( $games_database_id, 'game_publisher', $_POST['games_database_game_publisher'] );
    }
    if ( isset( $_POST['games_database_game_developer'] ) && $_POST['games_database_game_developer'] != '' ) {
        update_post_meta( $games_database_id, 'game_developer', $_POST['games_database_game_developer'] );
    }
    if ( isset( $_POST['games_database_rating'] ) && $_POST['games_database_rating'] != '' ) {
        update_post_meta( $games_database_id, 'game_rating', $_POST['games_database_rating'] );
    }
    if ( isset( $_POST['games_database_review_link'] ) && $_POST['games_database_review_link'] != '' ) {
        update_post_meta( $games_database_id, 'game_review_link', $_POST['games_database_review_link'] );
    }
    if ( isset( $_POST['games_database_game_release'] ) && $_POST['games_database_game_release'] != '' ) {
        update_post_meta( $games_database_id, 'game_release', $_POST['games_database_game_release'] );
    }
}
}

Can you see where I'm going wrong?

share|improve this question
    
Post your code please and explain what you are doing in more detail. –  s_ha_dum Aug 15 '13 at 13:28
    
@s_ha_dum done :) –  Dave Burns Aug 15 '13 at 13:34
    
I believe when you say "custom post functions" you mean "custom post meta". Yes? –  GhostToast Aug 15 '13 at 13:35
    
@GhostToast yeah sorry :) –  Dave Burns Aug 15 '13 at 13:43

1 Answer 1

up vote 2 down vote accepted

Your function is checking to see if anything "is set" before proceeding to update, and also that it isn't an empty string. Remove the "if" portions of your update statements, or better yet, put an "else" after to delete post meta if it is empty.

Example:

if ( isset( $_POST['games_database_game_release'] ) && $_POST['games_database_game_release'] != '' ) {
    update_post_meta( $games_database_id, 'game_release', $_POST['games_database_game_release'] );
} else {
    delete_post_meta( $games_database_id, 'game_release');
}
share|improve this answer
    
Okay so this is my code now so I've added that to my code but it still isn't working? –  Dave Burns Aug 15 '13 at 13:48
    
do I need to add the } else { line after each if? –  Dave Burns Aug 15 '13 at 13:51
    
^^ Yes, each if needs an else on the same pattern as the example. –  s_ha_dum Aug 15 '13 at 13:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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