Title: SQL Query to include/exclude a category
Last modified: August 19, 2016

---

# SQL Query to include/exclude a category

 *  [nathaner](https://wordpress.org/support/users/nathaner/)
 * (@nathaner)
 * [15 years, 5 months ago](https://wordpress.org/support/topic/sql-query-to-includeexclude-a-category/)
 * I was able to extend a query found here: [http://wordpress.org/support/topic/actual-query-to-get-posts-from-one-category-general-sql-question](http://wordpress.org/support/topic/actual-query-to-get-posts-from-one-category-general-sql-question)
   
   so you can search by category slug name instead of category id. Here’s what the
   beast looks like:
 *     ```
       SELECT ID,post_title,UNIX_TIMESTAMP(post_date) as date,comment_count
       FROM wp_posts AS wpost
       INNER JOIN wp_term_relationships ON ( wpost.ID = wp_term_relationships.object_id )
       INNER JOIN wp_term_taxonomy ON ( wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id )
       AND wp_term_taxonomy.taxonomy = 'category'
       INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id)
       WHERE post_status = "publish" AND post_type = "post" AND wp_terms.slug != 'CATEGORY_NAME'
       ```
   
 * Just wanted to share with everyone since I’ve found so many answers here.

The topic ‘SQL Query to include/exclude a category’ is closed to new replies.

## Tags

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

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 0 replies
 * 1 participant
 * Last reply from: [nathaner](https://wordpress.org/support/users/nathaner/)
 * Last activity: [15 years, 5 months ago](https://wordpress.org/support/topic/sql-query-to-includeexclude-a-category/)
 * Status: not a support question

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
