Title: Create query based on recent comments
Last modified: March 19, 2023

---

# Create query based on recent comments

 *  Resolved [kexikus](https://wordpress.org/support/users/kexikus/)
 * (@kexikus)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/)
 * I’m currently using WordPress Popular Posts with a custom query to get the top
   views posts of the week as described [here](https://wordpress.org/support/topic/how-to-sorting-a-custom-query-by-views-all-time-monthly-weekly-or-daily/).
   Is it possible to do something similar for the most commented posts of the week?
 * From what I can see there is no equivalent to wpp_get_views().
 * Can I use wpp_get_mostpopular() and then loop through the result to display them
   in a way I want? From my admittedly limited testing, it looks like this will 
   just display them directly, right?
 * Thanks for your help!

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16574437)
 * Hi [@kexikus](https://wordpress.org/support/users/kexikus/),
 * Well, yeah you’re right. No one has ever asked for a comments equivalent of wpp_get_views()
   haha. Might implement that at some point in the future, we’ll see.
 * As for how to get the most commented posts within a specific time range, you 
   could use the [WordPressPopularPosts\Query](https://github.com/cabrerahector/wordpress-popular-posts/blob/6.1.1/src/Query.php)
   class for that and loop the array it returns to build your custom popular posts
   list. The PHP comment section at the top of the file should help you get started
   but feel free to ask any questions you may have.
 *  Thread Starter [kexikus](https://wordpress.org/support/users/kexikus/)
 * (@kexikus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16699383)
 * Hi,
 * first of all thank you for the reply. I only got to give it a shot yesterday 
   but so far I have not been able to get it to work.
 * The query itself seems to work but so far I have not been able to successfully
   loop over the posts in that query. For now, I just want to display their titles
   to check if everything is working. With a standard WordPress query I used a while
   loop and the have_posts() function, i.e.,
 *     ```wp-block-code
       while ( $the_query->have_posts() ) {
       	$the_query->the_post();
       	$output .= '<li>' . get_the_title() . '</li>';
       }
       ```
   
 * But that does not seem to work with the WPP query. I also tried a foreach loop
   that also works with the default query but not with the WPP query.
 *     ```wp-block-code
       $posts = $the_query->posts;
       foreach( $posts as $post ) {
       	$output .= '<li>' . $post->post_title '</li>';
       }
       ```
   
 * For the WPP query I replaced the first line with
 *     ```wp-block-code
       $posts = $the_query->get_posts();
       ```
   
 * But that did not work either even though I confirmed that get_posts() successfully
   returns an array. Nevertheless, apparently I misunderstood what the WPP query
   returns and thus have been unable to implement it.
 * Any help would be very much appreciated.
 *  Thread Starter [kexikus](https://wordpress.org/support/users/kexikus/)
 * (@kexikus)
 * [3 years, 1 month ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16699764)
 * Nevermind, I figured it out. It should just be
 *     ```wp-block-code
       $post->title
       ```
   
 * in the foreach loop. Or $post->id to get the post ID.
 *  [jamin84](https://wordpress.org/support/users/jamin84/)
 * (@jamin84)
 * [3 years ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16802514)
 * [@kexikus](https://wordpress.org/support/users/kexikus/) To add on to your great
   work, I found you can treat it like a regular WordPress Loop if you add the following:
 * `<?php foreach ( $query_posts as $query_post ) : $post = get_post($query_post-
   >id); setup_postdata( $post );?>`
 * And now you can use all the great template tags like `the_title();` and `the_post_thumbnail()`
   within the `foreach`.
 * Cheers!
 *  [jensiboy1977](https://wordpress.org/support/users/jensiboy1977/)
 * (@jensiboy1977)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16853709)
 * that feature would be awesome

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Create query based on recent comments’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

## Tags

 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [setup_postdata](https://wordpress.org/support/topic-tag/setup_postdata/)

 * 5 replies
 * 4 participants
 * Last reply from: [jensiboy1977](https://wordpress.org/support/users/jensiboy1977/)
 * Last activity: [2 years, 11 months ago](https://wordpress.org/support/topic/create-query-based-on-recent-comments/#post-16853709)
 * Status: resolved