WordPress: Batch add categories with SQL query via PhpMyAdmin

spiros

  • Administrator
  • Hero Member
  • *****
    • Posts: 821560
    • Gender:Male
  • point d’amour
Batch add categories in WordPress with SQL query via PhpMyAdmin

Wordpress: SQL query to add or remove categories on multiple posts

What worked for me was:
Code: [Select]
INSERT INTO `wp_term_relationships` (object_id, term_taxonomy_id, term_order)
  SELECT ID, 42, 0 FROM `wp_posts` p
    WHERE p.post_type = 'post' AND p.ID NOT IN
      (SELECT object_id FROM wp_term_relationships tr3
        WHERE tr3.term_taxonomy_id = 42);

42 is the category number. And then I had to go to wp_term_taxonomy and change the number for the specific category to the one displayed on wp_term_relationships. Using MariaDB 10.3.
« Last Edit: 01 Jan, 2021, 18:47:54 by spiros »


 

Search Tools