计算逻辑出现不正确的 Wordpress 获取字段

Calculation Logic Coming out Incorrect Wordpress Get Field

我创建了一个评论小部件,它收集网站上的所有评论,然后通过计算创建一个综合评分。它似乎在大约 7 条评论和以下评论中工作得很好,但我最近添加了更多评论使其成为 13 条,现在计算似乎不正确,我不确定为什么有人可以提供帮助?

编辑:将代码更新为循环外,未解决问题。

<?php 
    $post_args = array(
        'post_type' => 'review'
    );

    $title = apply_filters( 'widget_title', $instance['title'] );
    $code = apply_filters( 'widget_code', $instance['code'] );
    $desc = apply_filters( 'widget_desc', $instance['desc'] );
    $link = apply_filters( 'widget_link', $instance['link'] );

    $total_rating = 0; //Sets the total rating to 0
    $post_list = new wp_query( $post_args ); //collects reviews

    if( $post_list->have_posts() ) : while( $post_list->have_posts() ) : 
    $post_list->the_post(); //initiates reviews posts

    $all_ratings = get_field('review_score'); //collects all integers
    $total_rating += $all_ratings; //gets all integers and adds them together

    ?>

    <?php endwhile; endif; 
    $multiply = $total_rating * 20; //Multiply total rating by 20 for width
    $total_reviews = wp_count_posts( 'review' )->publish; //Counts published reviews
    $width = $multiply / $total_reviews; //Divides multiply by amount of review for accurate % width of div.
    $divide = number_format($total_rating / $total_reviews, 1); //Divides the total of all reviews by amount of reviews e.g. 2 Review both 5/5 = 10/2=5=Correct.
    wp_reset_query(); ?>
    <?php endwhile; endif; wp_reset_query(); ?>

仅供参考 - get_field('review_score');作为评论分数的整数输入。

编辑:显示显示小部件的代码

<aside class="widget widget_reviews">
        </span>
        <h3><?php echo $title; ?></h3>
        <?php if($width > 0) : ?>
        <div itemscope itemtype="https://schema.org/Product">
            <meta itemprop="name" content="Regency Chauffeurs">
        <div itemprop="aggregateRating" itemscope itemtype="https://schema.org/AggregateRating">
            <meta itemprop="worstRating" content="1">
            <div class="average-reviews-base">
                <div class="average-reviews" style="width:<?php echo $width; ?>%"></div>
            </div>
            <span class="average-score-link"><span itemprop="ratingValue"><?php echo $divide; ?></span> / <span itemprop="bestRating">5</span> (From <a href="<?php echo $link; ?>" title="<?php echo $title; ?>"><span itemprop="reviewCount"><?php echo $total_reviews; ?></span> <?php if($total_reviews > 1) : ?>Reviews<?php else : ?>Review<?php endif;?></a>)</span>
        </div>
            <div class="review-desc">
                <p><?php echo $desc; ?></p>
                <p><a href="<?php echo $link; ?>" title="<?php echo $title; ?>" class="button"><?php echo $code; ?></a></p>
            </div>
        <?php else : ?>
        <div class="review-desc">
            <p>There is currently no reviews, if you would like to leave a review then please <a class="underline" href="<?php echo $link; ?>" title="review">click here</a>.</p>
            <p><a href="<?php echo $link; ?>" title="<?php echo $title; ?>" class="button">Leave A Review</a></p>
        </div>
        </div>
        <?php endif;?>
    </aside>

下图解释了 width 变量,golden star BG 是由 $width 变量改变的,以便给出准确的总评分图​​形。

在循环之后移动你的计算,只获取已发布的评论,将 posts_per_page 参数更改为 -1 以获得 所有 个评论帖子

<?php 
        $post_args = array(
            'post_type' => 'review',
            'post_status'=>'publish',
            'posts_per_page'=> -1
        );

        $total_rating = 0; //Sets the total rating to 0
        $post_list = new wp_query( $post_args ); //collects reviews

        if( $post_list->have_posts() ) : while( $post_list->have_posts() ) : 
        $post_list->the_post(); //initiates reviews posts

        $all_ratings = get_field('review_score'); //collects all integers
        $total_rating += $all_ratings; //gets all integers and adds them together

    ?>

    <?php endwhile; endif; 

$multiply = $total_rating * 20; //Multiply total rating by 20 for width
        $total_reviews = wp_count_posts( 'review' )->publish; //Counts published reviews
        $width = $multiply / $total_reviews; //Divides multiply by amount of review for accurate % width of div.
        $divide = number_format($total_rating / $total_reviews, 1); //Divides the total of all reviews by amount of reviews e.g. 2 Review both 5/5 = 10/2=5=Correct.

wp_reset_query(); ?>