Title: Top Articles code, please look
Last modified: August 20, 2016

---

# Top Articles code, please look

 *  [TFolder](https://wordpress.org/support/users/tfolder/)
 * (@tfolder)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/top-articles-code-please-look/)
 * Hey guys!
 * The theme has its own Top Articles sidebar next to the featured section, its 
   written into header.php. Its suppose to display articles with most comments, 
   but it doesn’t – instead, it displays the most recent articles including DRAFTS,
   that are not even published. If you click that it obviously gives the 404 error,
   because there is no article published yet.
 * Can anyone help? Here is the code for Top Articles in header.php
 *     ```
       <div class="top">
       					<span class="heading1"><span>Top Articles</span></span>
       					<ul>
       					<?php
                                               $kt=time()-1209600;
       						$sql = "SELECT * FROM $wpdb->posts WHERE post_date_gmt > FROM_UNIXTIME(".$kt.") AND post_type = 'post' 	ORDER BY comment_count DESC LIMIT 5";
       						$top_posts = $wpdb->get_results($sql);
   
       						foreach ($top_posts as $post)  :
       							?>
       							<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span><a class="comments" href="<?php the_permalink(); ?>#comments"><?php echo $post->comment_count; ?> Comments</a>Posted Under: <?php the_category(', '); ?></span></li>
                                                               <?php
       						endforeach;
       					?>
       					</ul>
       				</div>
       				<div class="clear"></div>
       			</div>
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [dbyington](https://wordpress.org/support/users/dbyington/)
 * (@dbyington)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/top-articles-code-please-look/#post-2774276)
 * If this is supposed to be the top 5 commented posts, irregardless of date posted…
   
   Try changing; `$sql = "SELECT * FROM $wpdb->posts WHERE post_date_gmt > FROM_UNIXTIME(".
   $kt.") AND post_type = 'post' ORDER BY comment_count DESC LIMIT 5";`
 * To;
    `$sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status
   = 'publish' ORDER BY comment_count DESC LIMIT 5";`
 * The key is selecting published posts, rather than all.
 * There is probably a better way to do this than hitting the DB directly. Maybe
   someone else knows a built-in function to do this.
 * Don
 *  Thread Starter [TFolder](https://wordpress.org/support/users/tfolder/)
 * (@tfolder)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/top-articles-code-please-look/#post-2774279)
 * Awesome, worked like a charm! Thanks!
 *  [dbyington](https://wordpress.org/support/users/dbyington/)
 * (@dbyington)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/top-articles-code-please-look/#post-2774286)
 * You’re welcome.
    Glad I could help.
 * Don

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Top Articles code, please look’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [dbyington](https://wordpress.org/support/users/dbyington/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/top-articles-code-please-look/#post-2774286)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
