admin管理员组

文章数量:1430551

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

I want to change the term_taxonomy_id of this query.

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

I want to change the term_taxonomy_id of this query.

Share Improve this question edited May 2, 2019 at 14:00 Tweak asked May 2, 2019 at 12:41 TweakTweak 311 silver badge8 bronze badges 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 Commented May 2, 2019 at 13:02
  • Also, is there a particular reason this isn't being done via WP CLI? – Tom J Nowell Commented 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 Commented 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 Commented May 2, 2019 at 13:07
  • 1 I have edited my post, maybe it's better for the comprehension now. – Tweak Commented May 2, 2019 at 13:53
 |  Show 5 more comments

1 Answer 1

Reset to default 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.

本文标签: wp queryMigrate posts from category and subcategory via SQL