0

I am trying to migrate all contents from a category to another using SQL. Thanks for help.

enter image description here

I want to change the term_taxonomy_id of this query.

10
  • What's the reasoning behind doing it in SQL? Would it not be faster to just rename 2018 to 2017 instead of moving each post? Then you'd only have to create 1 term for the latest year and one for the oldest year
    – Tom J Nowell
    May 2, 2019 at 13:02
  • Also, is there a particular reason this isn't being done via WP CLI?
    – Tom J Nowell
    May 2, 2019 at 13:03
  • Thanks for your answer @TomJNowell , the reason is that i have many posts from 2008 to today. I want to create a script to archive each of these posts to the corresponding year.
    – Tweak
    May 2, 2019 at 13:05
  • My query give me a line, for example. How can i change corresponding attributs of this line which is the result of my query "SELECT * ..." ?
    – Tweak
    May 2, 2019 at 13:07
  • 1
    I have edited my post, maybe it's better for the comprehension now.
    – Tweak
    May 2, 2019 at 13:53

1 Answer 1

1

I strongly suspect the reason you want SQL that you haven't mentioned/shared, is because you can't use the WP functions to fetch the posts and do it manually in a single request due to the PHP time execution limit, or you don't know how to.

Sadly, doing it via SQL is no guarantee this will work, additionally, you'll break all the caches involved, all the term post counts will be broken, and a few other things too.

The best way to do this, is with WP CLI, for example here we take all posts in category X, put them in category Y, then remove them from X:

# get all the post IDs in category x
posts=wp post list --category_name="x" --field="ID"

# Move them from x to Y
for id in $posts
do
  wp post term remove $id cat "x" --by="slug"
  wp post term add $id cat "y" --by="slug"
done

But given you've hinted you're actually doing this by year, it might be easier to simply rename the terms, so 2010 -> 2009, 2011 -> 2010 etc. This would be much much faster/reliable/simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.