$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Migrate posts from a Custom Post Type to another with SQL|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Migrate posts from a Custom Post Type to another with SQL

matteradmin12PV0评论

I would like to migrate all the posts from my custom post type "cpt-project" to another existing custom post type named "cpt-publication".

I don't want to use any plugin to achieve it. I would prefer to use a SQL query through phpMyAdmin to make the migration but I don't find any info on how do it.

Thanks.

I would like to migrate all the posts from my custom post type "cpt-project" to another existing custom post type named "cpt-publication".

I don't want to use any plugin to achieve it. I would prefer to use a SQL query through phpMyAdmin to make the migration but I don't find any info on how do it.

Thanks.

Share Improve this question asked Apr 16 at 16:50 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges 2
  • 1 what's the reason behind using just SQL? Doing it this way won't flush caches, update rewrite rules, or fire actions/filters that plugins and some themes would rely on to catch this. Otherwise there are shell/terminal/CLI options as well as PHP code options. Note that asking for a plugin recommendation isn't allowed here so people can't answer this question with the name of a plugin – Tom J Nowell Commented Apr 16 at 17:06
  • @TomJNowell Thanks for the suggestions. The only reason is because I only know SQL. As you mentioned it seems there are better options. – Mathieu Préaud Commented Apr 16 at 17:14
Add a comment  | 

2 Answers 2

Reset to default 1

WP CLI would be a better option. Assuming your host has it enabled, it shouldn't require installing any plugins.

wp post update $(wp post list --post_type=cpt-project) --post_type=cpt-publication

This searches for all posts of type cpt-project and updates their post type to cpt-publication.

The SQL answer:

UPDATE `wp_posts`
SET `post_type` = "cpt-publication"
WHERE `post_type` = "cpt-project"

This assumes your database table prefix is wp_. It will change the post type directly in the posts table.

As @tom-j-novell stated in the comments to your question, doing it this way does not invalidate caches or fire any hooks. But depending on your usecase that might be exactly what you are after.

Post a comment

comment list (0)

  1. No comments so far