Title: Modifying sql to pull posts from select categories
Last modified: August 20, 2016

---

# Modifying sql to pull posts from select categories

 *  [JGrizzLee](https://wordpress.org/support/users/jgrizzlee/)
 * (@jgrizzlee)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/modifying-sql-to-pull-posts-from-select-categories/)
 * I’m using this code to pull the most commented posts from the entire database.
   I was wondering how I can edit it so that it only pulls posts from a select few
   categories and not the entire database.
 *     ```
       <ul>
       					<?php
       						$sql = "
       							SELECT *
       							FROM $wpdb->posts
       							WHERE 	post_date_gmt > ".(time()-strtotime('-2 weeks'))." 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>
       							<?
       						}
       					?>
       					</ul>
       ```
   

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

 *  Thread Starter [JGrizzLee](https://wordpress.org/support/users/jgrizzlee/)
 * (@jgrizzlee)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/modifying-sql-to-pull-posts-from-select-categories/#post-2242286)
 * anybody? =/
 *  Thread Starter [JGrizzLee](https://wordpress.org/support/users/jgrizzlee/)
 * (@jgrizzlee)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/modifying-sql-to-pull-posts-from-select-categories/#post-2242305)
 * can a mod please move this to the advanced forum so it has a higher chance of
   being found by somebody who could help?

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

The topic ‘Modifying sql to pull posts from select categories’ is closed to new 
replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * 2 replies
 * 1 participant
 * Last reply from: [JGrizzLee](https://wordpress.org/support/users/jgrizzlee/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/modifying-sql-to-pull-posts-from-select-categories/#post-2242305)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
