Title: SQL Statement to join wp_post with Categories
Last modified: August 22, 2016

---

# SQL Statement to join wp_post with Categories

 *  [roxer82](https://wordpress.org/support/users/roxer82/)
 * (@roxer82)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-to-join-wp_post-with-categories/)
 * I need to get with SQL all the posts under a category, but I can’t make it because
   I don’t understand the tables.
    I need the post id, title, date, status = published
   under category X, Y or Z. How should I make the SQL statement?
 * Thanks.

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

 *  [Tunbosun Ayinla](https://wordpress.org/support/users/tubiz/)
 * (@tubiz)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-to-join-wp_post-with-categories/#post-5458785)
 * Hi roxer82,
    When getting posts from the WordPress database tables you are not
   to use SQL at all, WordPress comes with a WP_Query class that allows you to fetch
   posts based on a lot of conditions that you want to be met. To read more about
   it visit [http://codex.wordpress.org/Class_Reference/WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
 * To fetch the posts that met the criteria that you mentioned using the WP_Query
   class you use the snippet below.
 *     ```
       $args = array(
            'post_status'   => 'publish',
            'category__in'  => array(X,Y,Z), //where X,Y,Z are the category ids of the category you want to fetch post from
       );
   
       $the_query = new WP_Query( $args );
   
       if ( $the_query->have_posts() ) :
          while ( $the_query->have_posts() ) : $the_query->the_post();
             echo get_the_id() .'<br />'; //returns the post id
             echo get_the_date() .'<br />'; //returns the date the post was published
             echo get_the_title() .'<br />'; //returns the title of the post
          endwhile;
       endif;
       ```
   
 * Am sure this will serve as a starting point to help out.
 *  Thread Starter [roxer82](https://wordpress.org/support/users/roxer82/)
 * (@roxer82)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-to-join-wp_post-with-categories/#post-5458907)
 * Thanks for your answer Tunbosun Ayinla, but I need to use SQL because I need 
   to automatically change a lot of post titles from a category. It’s impossible
   to do it manually because I’m talking about hundreds of entries.

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

The topic ‘SQL Statement to join wp_post with Categories’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [roxer82](https://wordpress.org/support/users/roxer82/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/sql-statement-to-join-wp_post-with-categories/#post-5458907)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
