WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Most commented posts of the last 7 days (6 posts)

  1. feguwoz
    Member
    Posted 4 years ago #

    Hi everybody.

    I'm trying to show the most commented posts of the last week, but I'm almost giving up... No sucess. I use the timthumb together, to show automatic thumbnails of the posts at the header of my website.

    The hacks that I find don't work properly as I want. Sometimes I get the most commented posts of the week (without thumbnail), and sometimes the most commented posts ever (with thumbnail).

    This is the code that I'm using. I tried other codes, but as I said, they don't display the thumbnails. Never the most commented posts of the last week with thumbnails.

    <div id="top-posts">
        <?php $popular = new WP_Query('orderby=comment_count&posts_per_page=6'); ?>
    	<?php while ($popular->have_posts()) : $popular->the_post(); ?>
       	<div class="top-posts-2">
    	<?php $justanimage = get_post_meta($post->ID, 'Image', true);
    	if ($justanimage) { ?>
    	<?php } else { ?>
        <a href="<?php get_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php getImage('1'); ?>&w=140&h=100&zc=1" alt="thumbnail" /></a>
    	<?php } ?>
    	<h1>
    	<a href="<?php get_permalink(); ?>"><?php the_title(); ?></a>
        </h1>
        </div>
            	        <?php endwhile; ?>
        	    </div>

    Can somebody help me with it? Is there some special code that I could put there to show the most commented posts of the last week WITH the thumbnails working?

    Thanks in advance!

  2. feguwoz
    Member
    Posted 4 years ago #

    nobody?

  3. MichaelH
    Member
    Posted 4 years ago #

  4. MichaelH
    Member
    Posted 4 years ago #

    Actually, that plugin will only give a time frame on posts made in the last x days.

    Try this:

    <?php
    $weekstart = date('Y-m-d', strtotime('-7 days'));
    $query = $wpdb->prepare("
      SELECT comment_post_id, count( comment_post_id ) AS c, SUBSTRING( comment_date, 1, 10 ) AS d
      FROM        $wpdb->comments
      WHERE       (comment_date >= %s)
      GROUP BY comment_post_id
      ORDER BY c DESC , d DESC
      ",$weekstart);
    $col_ids = $wpdb->get_col($query);
    if ($col_ids) {
      $postids = implode($col_ids,', ');
    
      function filter_orderby($orderby = '') {
        global $postids;
        $orderby = " FIELD(ID,$postids )";
        echo $orderby;
        return $orderby;
      }
    
      add_filter('posts_orderby', 'filter_orderby');  //need this filter so posts get returned in the order we specify
    
      $args=array(
        'post__in' => $col_ids,
        'caller_get_posts'=>1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo 'List of Posts with most comments in last 7 days';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        endwhile;
      }
      remove_filter('posts_orderby', 'filter_orderby');
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
  5. feguwoz
    Member
    Posted 4 years ago #

    Thank you!! I'll try it!

  6. feguwoz
    Member
    Posted 4 years ago #

    It worked very well, man! Thank you so much!

    Actually, I had only to change the

    $orderby = " FIELD(ID,$postids )";

    to

    $orderby = "";

    It's working fine!

Topic Closed

This topic has been closed to new replies.

About this Topic