• Resolved nate.ads

    (@nateads)


    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 2 weeks ago by nate.ads.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Gemini Labs

    (@geminilabs)

    You can either use the [site_reviews_summary] shortcode or the glsr_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.

    Thread Starter nate.ads

    (@nateads)

    @geminilabs Thank you so much, the shortcode version worked perfect. I cannot believe how simple it was.

    Plugin Author Gemini Labs

    (@geminilabs)

    @nateads You’re welcome! If you are enjoying working with Site Reviews and have some time, please consider leaving a review. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.