WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On the "home"-page I'm trying to display the most recently commented-on post for each category.

I managed to display the most recently published post for each category, but if an older post from each category gets a comments, that post should be bumped to the top of the list with that latest comment displayed.

Here's the code I tried:

<?php $cat_args=array(
  'exclude'=> 12,
  'orderby' => 'name',
  'order' => 'ASC'
  foreach($categories as $category) {
      'showposts' => 1,
      'category__in' => array($category->term_id),
      if ($posts) {
        echo '<div class="pffcat"><h2><a href="' . get_category_link( $category->term_id ) . '">' . $category->name.'</a></h2> ';
        foreach($posts as $post) {
          setup_postdata($post); ?>

            <?php $recent_reviews = get_comments('post_id='.$post->ID.'&status=approve&number=1');?>

                  foreach($recent_reviews as $recent_review) {
                    $comment_url = get_permalink($recent_review->comment_post_ID).'#reviews';
                    $comment_info = get_comment_meta($recent_review->comment_ID, 'comment_info', true);
                    $comment_rating = get_comment_avg_rating($comment_info);

                <div id="post" class="post">
                  <div class="entry-summary">
                    <p><strong>Restaurant: <a href="<?php echo get_permalink($recent_review->comment_post_ID); ?>"><?php echo get_the_title($recent_review->comment_post_ID); ?></a></strong></p>
                    <p><?php echo get_limit_content(strip_tags($recent_review->comment_content), 150); ?></p>
                    <p><a href="<?php echo $comment_url; ?>" class="more-link">read review</a></p>
                  </div><!-- .entry-summary -->
                </div><!-- #post-## -->
                <div class="searchbox-r-home">
                  <img alt="" src="<?php bloginfo('template_directory'); ?>/images/star-m-<?php echo $comment_rating; ?>.png" />
                  <span><?php echo date('m/d/Y', strtotime($recent_review->comment_date)); ?></span>
                <?php } // foreach($recent_reviews as $recent_review) { ?>
        echo '</div><!--end box -->';// foreach($posts
      } // if ($posts
    } // foreach($categories
share|improve this question
do you want to show the recent comments from each category or the latest commented post from each category? like sorted by comments? – Pontus Abrahamsson Jul 29 '12 at 10:02
To be more specific, the setup is Neighbourhoods as categories, Restaurants as posts, Reviews as comments. On homepage I list the latest review, just one, for a restaurant (displaying just the name of the restaurant as post title not the whole post and the followed by the review) for every Neighbourhood (category) The problem is that every query I've tried displays the latest review for he latest added restaurant (post) to the homepage. I need the list to update with reviews added for restaurants published at a previous date. Basically a sort by latest comment in the output of the query. – pff Jul 29 '12 at 10:42
Please make an edit to your question and show the code you already tried. – kaiser Jul 29 '12 at 13:00
Added the code I tried. – pff Jul 29 '12 at 16:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.