Title: sql select posts matching multiple categories
Last modified: August 19, 2016

---

# sql select posts matching multiple categories

 *  Resolved [trevorhartman](https://wordpress.org/support/users/trevorhartman/)
 * (@trevorhartman)
 * [17 years ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/)
 * Hi,
 * I’m trying to figure the sql to select posts matching more than 1 category (ie.
   the post must match every category listed).
 * Here’s the SQL to select one category but I can’t figure out how to make it match
   multiple categories.
 *     ```
       SELECT p.post_title, p.ID, wpr.object_id, wp_terms.term_id, wp_terms.name, wp_term_taxonomy.taxonomy
   
       		FROM wp_terms
   
       		INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
   
       		INNER JOIN wp_term_relationships wpr ON wpr.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
   
       		INNER JOIN wp_posts p ON p.ID = wpr.object_id
   
       		WHERE taxonomy =  'category'
   
       		AND p.post_type =  'post'
   
       		AND p.post_status =  'publish'
   
       		AND (wp_terms.slug = 'surface-level') ORDER BY object_id
       ```
   
 * thanks, Trevor

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

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029461)
 * [http://codex.wordpress.org/Template_Tags/query_posts](http://codex.wordpress.org/Template_Tags/query_posts)
 * **Multiple Category Handling**
 * Display posts that are in multiple categories. This shows posts that are in both
   categories 2 and 6:
 * query_posts(array(‘category__and’ => array(2,6)));
 *  Thread Starter [trevorhartman](https://wordpress.org/support/users/trevorhartman/)
 * (@trevorhartman)
 * [17 years ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029667)
 * thanks for your reply, that looks like exactly what I need. the reason I was 
   rolling my own SQL is because i’m using amfphp to pull posts data into flash.
   being a wordpress noob, i’ve been reading up on The Loop, but i’m not sure how
   that would fit into the FlashPress amfphp service i’m using.
 * i’m doing it like this:
    1. mysql_query 2. loop results, parse columns into a
   dataobject 3. return array of typed dataobject
 * which ends up being an array of typed data objects in flash/as3.
 * so, with that in mind, is there a way to do this type of syntax while still using
   query_posts?
 * while( ( $row = mysql_fetch_array( $result ) ) != false )
 * many thanks – trevor
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029690)
 * Its not an area I have delved into, but you should be able to use query posts
   and then do the rest of your processing inside the WP loop
 *     ```
       <?php $recent = new WP_Query("cat=1&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
       	<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
       	<?php the_content(__('Read the story &raquo;'));?><div style="clear:both;"></div>
       <?php endwhile; ?>
       ```
   
 * is the standard code for a regular custom WP loop. However there are endless 
   numbers of lower level WP functions you have access to that you can use within
   the loop.
 * You might find this useful [http://codex.wordpress.org/Query_Overview](http://codex.wordpress.org/Query_Overview)
   And it has links to other useful query info.
 * [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
 * [http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query](http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query)
 *  [sinans11](https://wordpress.org/support/users/sinans11/)
 * (@sinans11)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029848)
 * Hi can any one help out on this sql code.
 * This sql code gets all the posts that are posted
    `code`$islem = mysql_query(“
   SELECT * FROM wp_posts Where post_type=’post’ AND ORDER BY ID DESC Limit 0,10”);`
   code`.
 * Can anyone help so that I can get posts from a specific category. I know a little
   about sql but wordpress 2.9 table format is too complicated for me. I think I
   need to use the wp_term_taxonomy and wp_terms tables but I don’t know the exact
   coding. can someone help please.
 *  [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * (@jon-sacci)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029853)
 * Hi Sinans11,
    Don’t forget to check if the post is published. (post_status = 
   publish). Unless of course you really want 10 posts regardless if it is published
   or not. =)
 * I use the following guery to get posts under a specific category. I got it by
   googling, and just added the $cat variable to hold the category id number, instead
   of defining it in the query. This way I can change the category id dynamically,
   while using the same query for different categories I want.
 *     ```
       $tp = $wpdb->prefix;  //-- the table prefix "wp_" etc.
   
       $cat = 8; //-- id of the category
   
       $cat_query = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id =$cat) ORDER BY post_date DESC");
       ```
   
 *  [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * (@jon-sacci)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029854)
 * Pardon my spelling. guery is obviously supposed to be query… =P
    ..and “if it
   is” should be “if they are”.. Oh well, it’s Monday alright. =D
 *  [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * (@jon-sacci)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029855)
 * Fast googling also resulted to the following post.
    [http://www.asherbond.com/blog/2009/05/21/how-to-select-related-posts-by-tags-and-category-relevance-in-wordpress/](http://www.asherbond.com/blog/2009/05/21/how-to-select-related-posts-by-tags-and-category-relevance-in-wordpress/)
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029856)
 * As stvwlf noted there is no need to resort to [wpdb](http://codex.wordpress.org/wpdb)
   to do that:
 *     ```
       <?php
       $args=array(
         'category__in' => array(1,2,3), // 1,2,3 would be the cat ids
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => -1,
         'caller_get_posts'=> 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         echo 'List of Posts in category 8';
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
         endwhile;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * (@jon-sacci)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029857)
 * Geez. Thanks Michael. That makes more sense. Is the `$my_query = null;` mandatory
   or just a good practise? Did I understand correctly, that `wp_reset_query();`
   continues the loop where we left off before our special query?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029858)
 * Good practice.
 * [Function_Reference/wp_reset_query](http://codex.wordpress.org/Function_Reference/wp_reset_query)
 *  [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * (@jon-sacci)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029859)
 * Great. Thanks again Michael. It’s a continuous learning curve. =)

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

The topic ‘sql select posts matching multiple categories’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 11 replies
 * 5 participants
 * Last reply from: [jon-sacci](https://wordpress.org/support/users/jon-sacci/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/sql-select-posts-matching-multiple-categories/#post-1029859)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
