Cray
Cray

Reputation: 5483

WooCommerce: Get reviews from products in array and sort them by stars

I want to show some reviews based on a list of products. The product IDs are stored in an array. So I tried the following:

<?php
$args = array (
    'post_type' => 'product',
    'number'  => '5',
    'post_id' => array('2360','2362'),
);
$comments = get_comments( $args );
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>

It looks, that post_id does not allow an array of IDs. If I try it with only one ID it works fine.

EDIT: I found a way to order by star rating:

'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC'

Upvotes: 3

Views: 1299

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253814

to show to show some reviews based on a specific products, you need to use post__in argument instead of post_id like:

<?php
$comments = get_comments( array (
    'number'    => '5',
    'post__in'  => array('2360','2362'), // <= HERE your array of product Ids
    'post_type' => 'product',
    'meta_key'  => 'rating',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC'
) );

wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>

It should works now. See WP_Comment_Query available parameters.

Upvotes: 3

Related Questions