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?