Pulling reviews using WP_Query
-
I am using a WP_Query to display a list of products using a CPT. I am trying to figure out how to pull in the review stars only for each product.
My code looks like this for the product list<?php
$args = array(
'posts_per_page' => 4,
'post_type' => 'products',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="product-list-container <?php echo implode(' ,', wp_get_post_tags( get_the_ID(), array('fields' => 'names') ) ); ?>">
<a href="<?php the_permalink() ?>">
<div class="img-place"><img src="<?php echo get_the_post_thumbnail_url( get_the_ID(), 'full' ); ?>" alt="<?php the_title(); ?>" /></div>
<span><?php the_title(); ?></span>
* Display review stars here *
</a>
</div>
<?php endwhile;
wp_reset_postdata();
?>-
This topic was modified 6 months ago by
nate.ads.
-
This topic was modified 6 months ago by
-
You can either use the
[site_reviews_summary]
shortcode or theglsr_star_rating
function.For example:
echo do_shortcode('[site_reviews_summary assigned_posts="post_id" hide="rating,summary,bars"]');
Or:
$postId = get_the_ID();
$rating = get_post_meta($postId, '_glsr_average', true);
$reviewCount = get_post_meta($postId, '_glsr_reviews', true);
echo apply_filters('glsr_star_rating', null, $rating, $reviewCount);You don’t need to cast the meta values to an integer/float as the helper function will sanitize the values for you.
And, if you want to sort your WP_Query by the average rating or the bayesian ranking, please see the examples on the Site Reviews > Help & Support > FAQ page.
-
This reply was modified 6 months ago by
Gemini Labs.
@geminilabs Thank you so much, the shortcode version worked perfect. I cannot believe how simple it was.
@nateads You’re welcome! If you are enjoying working with Site Reviews and have some time, please consider leaving a review. 🙂
-
This reply was modified 6 months ago by
- The topic ‘Pulling reviews using WP_Query’ is closed to new replies.