wp cli - How do I clone or duplicate a post with the WordPress Command Line Interface WP-CLI?

I know that I can clone or duplicate a post with several plugins. The one I use most often is . However I am using the

I know that I can clone or duplicate a post with several plugins. The one I use most often is /. However I am using the WP-CLI more and more lately and I would like to be able to duplicate or clone a post from the command line.

Any suggestions would be appreciated.

I know that I can clone or duplicate a post with several plugins. The one I use most often is https://wordpress/plugins/duplicate-post/. However I am using the WP-CLI more and more lately and I would like to be able to duplicate or clone a post from the command line.

Any suggestions would be appreciated.

Share Improve this question asked Jan 20, 2017 at 3:35 tfrangiotfrangio 631 silver badge4 bronze badges 2
  • Copy a post is pretty simple, but copy the post meta can be a pita. I have created this gist gist.github/alejandrogih/fc79e22dd7d9c65048689a4bc12e0dce Hope it helps to someone else. – Alejandro Giraldo Commented Feb 13, 2020 at 8:04
  • See @cfx for the correct answer now that this is supported directly by wp-cli – mrwweb Commented Feb 19 at 17:07
Add a comment  | 

3 Answers 3

Reset to default 6

Cloning a post via wp-cli is little trickier. It needs two steps:

  1. Create a file where save the information of post. Suppose, following command create a file named file.txt from hello post (id 1). In this case file.txt which save on root directory.

    wp post get 1 > file.txt

  2. Create new post from this file. In our scenario file.txt saved all information of hello post. Following command create a post named duplicate

    wp post create ./file.txt --post_title="duplicate"

This can now be done via WP-CLI using $ wp post create --from-post=1. It even dupes the meta data.

Thanks a lot for your answer and @Mrinal and confirming it would work with a pipe @tfrangio.

I was interested too in copying the meta values from one post to the other, so after creating the post with the duplicate with the pipe:

wp post get 6815 --field=content | wp post create - --post_title="Title of dup" --post_status='draft'

After I get the ID of the new post, I passed the meta_keys which I was interested...

assuming 14 is the source post_ID and 21 the target post_ID, I want to copy tie_hide_related and tie_sidebar_pos:

for meta_key in tie_hide_related tie_sidebar_pos ; \
do wp post meta get 14 $meta_key | \
xargs wp post meta update 21 $meta_key \
; done

xargs is needed to pass the value returned by post-meta-get to wp post-meta update, even if the documentation says it reads from stdin, without xargs some single values are read as an one item list, thus changing the behaviour of the value

Then, to copy the terms of a taxonomy, let's say category:

 for cat_id in `wp post term list 14 category --format=ids` ; \
 do wp post term add 21 category $cat_id --by=id ; done

This last step could be repeated for several taxonomies.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744752220a4591665.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信