How to add category to all post by using sql query?

I have a thousand posts and all them don’t have category. So, Do you know how to associate category with all posts by using sql query ? Thank you !

Related posts

Leave a Reply

3 comments

  1. First you need to know the id of you category you want your posts to be associated with. Look in the table wp_terms column term_id.

    INSERT INTO wp_term_relationships (object_id, term_taxonomy_id)
    SELECT ID, 'TERM_ID' FROM wp_posts
    

    Replace TERM_ID with the number you found in wp_terms.

  2. don’t need to use mySql, you can do it with bulk actions in your wp admin

    1. Create your categories as usual.
    2. Go to your entries and select them all
    3. Select in bulk actions / edit
    4. Edit all at once.
  3. Mauro above suggested bulk editing which is your easiest way to go. I would suggest these steps:

    1. In the wordpress post editor, click on “Screen Options” and select 100 posts per page.
    2. Now you can edit 100 posts at a time. So click the checkbox by the “Title” to select all posts on the page.
    3. From the “bulk actions” dropdown select “edit” and click “Apply”
    4. Click the category you want the posts added to.
    5. Click “Update”!
    6. Do it again for page 2-10, as many as you have. By saving 100 at a time it’s just 10 saves to go through 1000 posts and it’s safer than touching the DB.