Skip to content

Instantly share code, notes, and snippets.

@collegeman
Forked from coreyweb/gist:2718955
Created May 18, 2012 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save collegeman/2726141 to your computer and use it in GitHub Desktop.
Save collegeman/2726141 to your computer and use it in GitHub Desktop.
Recent WordPress Posts by Comment Frequency
<?php
/*
@author Corey Brown https://github.com/coreyweb
This code will display the 10 most commented on posts.
Rules:
- published within the last month
- at least one comment made in the last week
*/
?>
<h3>Most Discussed <span>This Week</span></h3>
<?php
$result = $wpdb->get_results("
SELECT ID, comment_count, post_title
FROM {$wpdb->posts}
JOIN {$wpdb->comments} ON (comment_post_ID = ID)
WHERE
post_status = 'publish'
AND comment_approved = '1'
AND post_date_gmt > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 MONTH)
AND comment_date_gmt > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 1 WEEK)
GROUP BY ID
ORDER BY comment_count DESC
LIMIT 10
");
foreach ($result as $post) {
?>
<a href="<?php echo get_permalink( $post->ID ) ?>" title="Read the article" ><?php
echo $post->post_title
?></a>
<a href="<?php echo get_permalink( $post->ID ) ?>#comments" title="Read the comments"><?php
echo $post->comment_count
?></a>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment