WordPress.org

Ready to get started?Download WordPress

Forums

Displaying most commented in two weeks (2 posts)

  1. sh3nka7a
    Member
    Posted 1 year ago #

    Hello, im currently using this code to display the most commented on my blog, any way to make it display the most commented articles in just two weeks instead from begining?

    <?php
    $args = array(
    	'posts_per_page' => 5,
    	'orderby' => 'comment_count',
    	'order' => 'DESC'
    );
    	$recentposts = get_posts($args);
    		foreach ($recentposts as $post) :
    		setup_postdata($post);
    	?>
     <?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?>
    	<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
  2. Devtard
    Member
    Posted 1 year ago #

    Hello, this should do it:

    <?php
    function filter_posts($where = '') {
    		//posts in the last 14 days
    		$where .= " AND post_date > '" . date('Y-m-d', strtotime('-14 days')) . "'";
    		return $where;
    }
    
    add_filter('posts_where', 'filter_posts');
    
    $args = array(
    	'posts_per_page' => 5,
    	'orderby' => 'comment_count',
    	'order' => 'DESC'
    );
    
    $filter_posts_query = new WP_Query($args);
    remove_filter('posts_where', 'filter_posts');
    
    //loop for displaying posts
    while ($filter_posts_query->have_posts()) : $filter_posts_query->the_post();
    
    	the_post_thumbnail(array(50,50), array ('class' => 'alignleft'));
    ?>
    
    	<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags