Posted By

LuckyShot on 12/01/09


Tagged

sql wordpress loop wp


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vintagekrug
jlan


Wordpress - Custom loop SQL query (and Popular Posts example)


 / Published in: PHP
 

  1. <h2>Popular Posts</h2>
  2. <ul>
  3. <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
  4. foreach ($result as $post) {
  5. setup_postdata($post);
  6. $postid = $post->ID;
  7. $title = $post->post_title;
  8. $commentcount = $post->comment_count;
  9. if ($commentcount != 0) { ?>
  10. <li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
  11.  
  12. <?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
  13. <?php } } ?>
  14. </ul>

Report this snippet  

You need to login to post a comment.