• Resolved kexikus

    (@kexikus)


    I’m currently using WordPress Popular Posts with a custom query to get the top views posts of the week as described here. 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

    (@hcabrera)

    Hi @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 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

    (@kexikus)

    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.,

    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.

    $posts = $the_query->posts;
    foreach( $posts as $post ) {
    	$output .= '<li>' . $post->post_title '</li>';
    }

    For the WPP query I replaced the first line with

    $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

    (@kexikus)

    Nevermind, I figured it out. It should just be

    $post->title

    in the foreach loop. Or $post->id to get the post ID.

    @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!

    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.