• Resolved feguwoz

    (@feguwoz)


    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter feguwoz

    (@feguwoz)

    nobody?

    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().
    }
    ?>

    Thread Starter feguwoz

    (@feguwoz)

    Thank you!! I’ll try it!

    Thread Starter feguwoz

    (@feguwoz)

    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Most commented posts of the last 7 days’ is closed to new replies.