Custom Post Type rating not working

2019-09-16 13:20发布

问题:

Using the following code I managaed to add Rating to my custom post_type and I intend to show star signs according to number of rating:

function display_game_meta_box( $game ) {
    // Retrieve current name of the Author and Game Rating based on review ID
    $game_Author = esc_html( get_post_meta( $game->ID, 'game_Author', true ) );
    $game_rating = intval( get_post_meta( $game->ID, 'game_rating', true ) );
    ?>
    <table>
        <tr>
            <td style="width: 100%">Game Author</td>
            <td><input type="text" size="80" name="game_Author_name" value="<?php echo $game_Author; ?>" /></td>
        </tr>
        <tr>
            <td style="width: 150px">Game Rating</td>
            <td>
                <select style="width: 100px" name="game_rating">
                <?php
                // Generate all items of drop-down list
                for ( $rating = 5; $rating >= 1; $rating -- ) {
                ?>
                    <option value="<?php echo $rating; ?>" <?php echo selected( $rating, $game_rating ); ?>>
                    <?php echo $rating; ?> stars <?php } ?>
                </select>
            </td>
        </tr>
    </table>
    <?php
}

function my_admin() {
    add_meta_box( 'game_meta_box',
        'Game Details',
        'display_game_meta_box',
        'games', 'normal', 'high'
    );
}
add_action( 'admin_init', 'my_admin' );

Inside my template file I used this to view the starts according to their amount chosen:

<?php
$nb_stars = intval( get_post_meta( get_the_ID(), 'game_rating', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
    if ( $star_counter <= $nb_stars ) {
        echo 'star';
    } else {
        echo 'grey';
    }
}
?>

When I view the page I see that only the else statement is being executed. Another thing is that when I go select a rating in the backend, it keeps showing me 5 starts after updating even though is not 5 that I chose.

This is what I tried to save the metabox data:

function add_movie_review_fields( $game_id, $game ) {
    // Check post type for movie reviews
    if ( $game->post_type == 'games' ) {

        if ( isset( $_POST['game_rating'] ) && $_POST['game_rating'] != '' ) {
            update_post_meta( $game_id, 'games', $_POST['game_rating'] );
        }
    }
}

add_action( 'save_post', 'add_movie_review_fields', 10, 2 );

Is there anything that I might be doing wrong with the rating?

回答1:

$nb_stars is probably empty because you're not saving your meta with the proper key.

function add_movie_review_fields( $game_id, $game ) {
    // Check post type for movie reviews
    if ( $game->post_type == 'games' ) {

        if ( isset( $_POST['game_rating'] ) && $_POST['game_rating'] != '' ) {
            update_post_meta( $game_id, 'game_rating', $_POST['game_rating'] ); // changed meta key
        }
    }
}

add_action( 'save_post', 'add_movie_review_fields', 10, 2 );

The meta key you're updating must match the key you're fetching. Now $nb_stars should get the proper post meta value.