Horje
Displaying most discussed (commented) posts within past 7 days - Wordpress Solution
[Solved] - Displaying most discussed (commented) posts within past 7 days - Wordpress Solution
Try following codes
I have a simple question. A really simple question to be honest but I can't figure it out, no matter how often I go to wordpress.org and read up on their docs. All I want is to display the most commented posts within the past week (i.e. Mon-Sun) or the past 7 days (whichever is easier). I tried doing this already with a query within the loop but for some reason it's showing the most commented posts of all time even though I added some parameters to the query to specify the range of posts that I wanted to have covered. Can someone take a look at the code below and tell me what to change?



<ul>

<?php 

$week = date('W');
$year = date('Y');

$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w=$week&year=$year'); ?>

	<?php while ($popular->have_posts()) : $popular->the_post(); ?>	
	
	
	<li>

	<?php $justanimage = get_post_meta($post->ID, 'image', true);
			if ($justanimage) {
			?>
	<img align="left" src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", true); ?>&amp;w=75&amp;h=63&amp;zc=1" alt="<?php the_title(); ?>" />

	<?php } else { ?>

<img align="left" src="<?php bloginfo('template_directory'); ?>/images/no-thumb.png" width="75px" height="63px" alt="<?php the_title(); ?>" />

	<?php } ?>

	<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</li>
<?php endwhile; ?>



</ul>



Thanks, Brennen

Solution - 1

Replace:

$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w=$week&year=$year'); ?>
With:
$popular = new WP_Query('orderby=comment_count&posts_per_page=5&w='.$week.'&year='.$year.''); ?>
Enjoy


Solution - 2

something like this =)


<?php
global $wpdb;
$mostCommented = $wpdb->get_results("SELECT ID, post_title 
	FROM ".$wpdb->posts." WHERE 
		`post_status` = 'publish' 
	AND `post_type` = 'post' 
	AND `post_date` >= (SELECT date_SUB(MAX(post_date),  interval 8 DAY) 
		FROM ".$wpdb->posts." WHERE `post_status` = 'publish'  AND `post_type` = 'post' 
	) 
	ORDER BY comment_count DESC
	LIMIT 5");
?>
<ul>
<?php	foreach($mostCommented as $item){ ?>
	<li>
	<?php if (get_post_meta($item->ID, 'image', true)) { ?>
		<img align="left" src="<?php bloginfo('template_directory'); 
		?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, "image", true); 
			?>&amp;w=75&amp;h=63&amp;zc=1" alt="<?php echo apply_filters('the_title', $item->post_title); ?>" />
	<?php } else { ?>
		<img align="left" src="<?php bloginfo('template_directory'); 
			?>/images/no-thumb.png" width="75px" height="63px" alt="<?php 
				echo apply_filters('the_title', $item->post_title); ?>" />
	<?php } ?>
	<h2><a href="<?php echo get_permalink( $item->ID ); ?>">
		<?php echo apply_filters('the_title', $item->post_title); ?>
	</a></h2>
	</li>
<?php	} ?>	
</ul>	
simple and fast


Solution - 3

Use

$popular = new WP_Query("orderby=comment_count&posts_per_page=5&w=$week&year=$year");
Variables aren't parsed when in single quotes.


Solution - 4

In theory, this plugin should be able to do the heavy lifting for you: http://wordpress.org/extend/plugins/popularity-contest You set it up so you only use comment weight, and then use the above code to parse for the last 7 days.

Date: 2022-10-21 05:22:33
Category: Wordpress
More for You