Skip to content

Instantly share code, notes, and snippets.

@joshferrara
Created August 6, 2012 04:04
Show Gist options
  • Save joshferrara/3270107 to your computer and use it in GitHub Desktop.
Save joshferrara/3270107 to your computer and use it in GitHub Desktop.
Wordpress Random Comment
// setup everything
<?php
function get_random_comment($ID = 0) {
if( $ID == 0 ) {
global $post;
$ID = $post->ID;
}
global $wpdb;
return $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_ID > 9 ORDER BY RAND() LIMIT 1", $ID));
}
function setup_random_comment($ID = 0) {
global $comment;
$comment = get_random_comment($ID);
}
?>
// display comments
<?php
query_posts('page_id=231');
if (have_posts()) : while (have_posts()) : the_post();
setup_random_comment();
?>
<p>
<?php comment_text_rss(); ?>
</p>
<?php endwhile; endif; wp_reset_query(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment