Title: &#8220;Popular Posts&#8221; script that returns permalinks
Last modified: August 19, 2016

---

# “Popular Posts” script that returns permalinks

 *  [holdstrong](https://wordpress.org/support/users/holdstrong/)
 * (@holdstrong)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/popular-posts-script-that-returns-permalinks/)
 * Hi all,
 * Was wondering if any code gurus out there could help me tweak a bit of script
   I have been using. It is a script I include in my footer as a way to show “popular
   posts”.
 * My problem is that it displays results with the ‘ugly’ guid links. The p= kind.
   I would like it to return and display links using my permalinks.
 * Any thoughts?
 * Here is the code:
 *     ```
       <?php
       	$sql='SELECT post_title, comment_count, guid
       		FROM wp_posts WHERE post_status = "publish"
       		ORDER BY comment_count DESC
       		LIMIT 6;';
       	$results = $wpdb->get_results($sql);
   
       	foreach ($results as $r) {
       		echo '<li><a href="' . $r->guid . '" title="' . $r->post_title . '"> ' . $r->post_title .
       			' (' . $r->comment_count . ')</a></li>';
       	}
       ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [holdstrong](https://wordpress.org/support/users/holdstrong/)
 * (@holdstrong)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/popular-posts-script-that-returns-permalinks/#post-1346152)
 * Was able to answer my own question. Hope this helps someone else:
 *     ```
       <?php query_posts('orderby=comment_count&showposts=6');
          if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
             <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?> (<?php comments_number('0','1','%'); ?>)</a></li>
             <?php endwhile; ?>
             <?php else : ?>
             <li>Sorry, no posts were found.</li>
          <?php endif; ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘“Popular Posts” script that returns permalinks’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [holdstrong](https://wordpress.org/support/users/holdstrong/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/popular-posts-script-that-returns-permalinks/#post-1346152)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
