删除自定义POST函数值

时间:2013-08-15 作者:Dave Burns

目前我在自定义函数方面遇到了一个问题,一旦向数据库中添加了一个值,我就无法删除该值,解决这个问题的最佳方法是什么?

基本上,我已经为游戏数据库创建了一个自定义帖子类型,添加游戏时有几个自定义帖子功能;游戏开发者、发布者、评论分数和评论URL,现在如果我输入一个测试URL,它将保存该URL,但当我尝试删除它时,它仍会出现,并且不会保存删除的值。

下面是我使用的代码:

<?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\'] );
    }
}
}
你能看出我错在哪里吗?

1 个回复
最合适的回答,由SO网友:GhostToast 整理而成

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\');
}
结束

相关推荐