There's the WP_Comment_Query
for this task. Then we got get_all_category_ids()
.
So first we'd query all the products
custom post type posts (or whatever it's called).
$products = new WP_Query( array(
'cat' => get_all_category_ids()
,'posts_per_page' => -1
,'posts_per_archive_page' => -1
,'post_type' => 'products'
) );
This will allow us to do a simple loop:
if ( $products->have_posts() )
{
while( $products->have_posts() )
{
the_post();
// display thumbnail, product title, description, comments here
}
}
Then, before the loop, we'd fetch all comments and order them by ID:
$testimonials = new WP_Comment_Query( array(
'status' => 'approve'
,'orderby' => 'comment_post_ID'
,'number' => ''
) );
This will help us doing this query only once, so we save processing time.
Then, inside the loop, while displaying a single product, we'd fetch our comments/testimonials for this product, using wp_list_pluck()
by the comment_post_ID
"field":
$testimonials_for_product = wp_list_pluck( $testimonials, 'comment_post_ID' );
The final step would just be looping through them:
foreach ( $testimonials_for_product as $tfp )
var_dump( $tfp );
This code isn't tested, so it probably needs some testing and fine tuning.