Daniel Vickers
Daniel Vickers

Reputation: 1074

Calculation Logic Coming out Incorrect Wordpress Get Field

I have created a reviews widget that collects all the reviews on the website and then creates an aggregate rating via a calculation. It seemed to work perfectly fine with around 7 reviews and below however I recently added more to make it 13 and the calculation seems to now be incorrect and I am not sure why can anyone help?

EDIT: Updated code to be outside of loop, did not resolve the issue.

<?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(); ?>

FYI - get_field('review_score'); is input as an integer for the review score.

EDIT: Displaying the code that displays the widget

<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>

Below is an image to explain the width variable, the golden star BG is what is altered by the $width variable in order to give an accurate graphic of aggregate rating.

Review Width

Upvotes: 0

Views: 39

Answers (1)

madalinivascu
madalinivascu

Reputation: 32354

Move your calculation after the the loop, get only published reviews,change the posts_per_page param to -1 to get all the review posts

<?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(); ?>

Upvotes: 1

Related Questions