Title: Add category via SQL
Last modified: August 19, 2016

---

# Add category via SQL

 *  [trib](https://wordpress.org/support/users/trib/)
 * (@trib)
 * [18 years, 8 months ago](https://wordpress.org/support/topic/add-category-via-sql/)
 * Through a combination of circumstances – changing the IA/category structure of
   my blog, a move to mostly using tags, imports, exports, upgrades, etc. I have
   a situation where a large number of my posts have _no category_.
 * They’re not assigned the default “Uncategorised”; it doesn’t even exist for the
   purposes of my blog. They actually have no category.
 * What I want is a quick SQL script that will assign a known category to these 
   posts. I just need some help with the SQL to assign it.
 * Can anyone offer some ideas, please?

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

 *  Moderator [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [18 years, 8 months ago](https://wordpress.org/support/topic/add-category-via-sql/#post-682058)
 * If there is already the category (term), then you need to do this.
 * First, find the term in the terms table. Get its term_id.
 * Next, look in the term_taxonomy table to find its term_taxonomy_id. This will
   be the entry matching the term_id you found already and having a taxonomy of ‘
   category’.
 * Next, get the ID value from the posts table of all the posts you want to assign
   to that category.
 * Then do this for each of them:
    `INSERT INTO term_relationships (object_id, term_taxonomy_id)
   VALUES (POST_ID, TERM_TAXONOMY_ID);`
 * Obviously replace POST_ID and TERM_TAXONOMY_ID with the post id’s and the term_taxonomy_id
   numbers.
 *  [sneakersneaker](https://wordpress.org/support/users/sneakersneaker/)
 * (@sneakersneaker)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/add-category-via-sql/#post-682530)
 * The SQL query above worked great for adding categories to posts in bulk. However,
   the category count didn’t seem to update on its own. So there’s probably some
   additional SQL action needed to make it all sync up. Anyone have any pointers?
 * Thanks!
 *  [sneakersneaker](https://wordpress.org/support/users/sneakersneaker/)
 * (@sneakersneaker)
 * [18 years, 5 months ago](https://wordpress.org/support/topic/add-category-via-sql/#post-682531)
 * Aha… looks like this post has the solution: [http://wordpress.org/support/topic/130392?replies=2#post-648586](http://wordpress.org/support/topic/130392?replies=2#post-648586)

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

 The topic ‘Add category via SQL’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [sneakersneaker](https://wordpress.org/support/users/sneakersneaker/)
 * Last activity: [18 years, 5 months ago](https://wordpress.org/support/topic/add-category-via-sql/#post-682531)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
