0

Would anybody have a good idea for getting the WP to spit out all posts from a category with (approved) comments only?

I have an ecommerce site set up, and treat comments on product posts as testimonials.

Now I want a page that shows all those 'testimonials' (comments) next to the product (post) title and thumbnail.

Can anyone help?? Ive tried a number of things, but obviously dont understand the workings of the loop well enough to filter on these criteria....

Cheers! peter

2 Answers 2

0

To retrieve all posts from a category, while preserving the original Loop query, you create a new query like so:

<?php 

$testimonials = new WP_Query( 
    array( 
        'post_status' => 'publish', 
        'post_type' => 'post', 
        'posts_per_page' => 30,     // limit number of posts for which to get ALL the comments of
        'category__in' => array( 2, 6, 18 ) // specify the categories you want the posts of
    ) 
);

?>

The variable $testimonials now contains all posts from the categories specified. You can place this inside the current main loop. In single.php, that would look something like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // this is the main loop, used in single.php will only contain a single post ?>

<article id="post-<?php $post->ID; ?>">

    <h1><?php the_title(); ?></h1>
    <?php the_content(); // this gets the posts' own content ?>

    <?php $testimonials = new WP_Query( 
        array( 
            'post_status' => 'publish', 
            'post_type' => 'post', 
            'posts_per_page' => 30,     // limit number of posts for which to get ALL the comments of
            'category__in' => array( 2, 6, 18 ) // specify the categories you want the posts of
        ) 
    );
    if ( $testimonial->have_posts() ) : while ( $testimonial->have_posts() ) : $testimonial->the_post(); 

        // please note that $testimonial->the_post() has overwritten the original $post,
        // but since we're on single.php and the main post content was already fetched that's probably alright
        $comments = get_comments( array(
            'status' => 'approve',
            'post_id' => $post->ID, 
            'number' => '5', // if you want to limit the number of comments on that particular post ID
        ));

        foreach( $comments as $comment ) :
            echo( $comment->comment_author . '<br />' . $comment->comment_content );
        endforeach;

    endwhile; // this quits the $testimonials loop, not the main loop
    endif; // end of $testimonials->have_posts()
    ?>

</article>

<?php
endwhile; // this quits the main loop
endif; // end of have_posts()
?>
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.