@@ -78,6 +78,9 @@ public function __construct() {
7878 * [--post_name=<post_name>]
7979 * : The post name. Default is the sanitized post title when creating a new post.
8080 *
81+ * [--from-post=<post_id>]
82+ * : Post id of a post to be duplicated.
83+ *
8184 * [--to_ping=<to_ping>]
8285 * : Space or carriage return-separated list of URLs to ping. Default empty.
8386 *
@@ -147,6 +150,10 @@ public function __construct() {
147150 * # Create a post with multiple meta values.
148151 * $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
149152 * Success: Created post 1923.
153+ *
154+ * # Create a duplicate post from existing posts.
155+ * $ wp post create --from-post=123 --post_title='Different Title'
156+ * Success: Created post 2350.
150157 */
151158 public function create ( $ args , $ assoc_args ) {
152159 if ( ! empty ( $ args [0 ] ) ) {
@@ -173,6 +180,27 @@ public function create( $args, $assoc_args ) {
173180 $ array_arguments = array ( 'meta_input ' );
174181 $ assoc_args = \WP_CLI \Utils \parse_shell_arrays ( $ assoc_args , $ array_arguments );
175182
183+ if ( isset ( $ assoc_args ['from-post ' ] ) ) {
184+ $ post = $ this ->fetcher ->get_check ( $ assoc_args ['from-post ' ] );
185+ $ post_arr = get_object_vars ( $ post );
186+ $ post_id = $ post_arr ['ID ' ];
187+ unset( $ post_arr ['post_date ' ] );
188+ unset( $ post_arr ['post_date_gmt ' ] );
189+ unset( $ post_arr ['guid ' ] );
190+ unset( $ post_arr ['ID ' ] );
191+
192+ if ( empty ( $ assoc_args ['meta_input ' ] ) ) {
193+ $ assoc_args ['meta_input ' ] = $ this ->get_metadata ( $ post_id );
194+ }
195+ if ( empty ( $ assoc_args ['post_category ' ] ) ) {
196+ $ post_arr ['post_category ' ] = $ this ->get_category ( $ post_id );
197+ }
198+ if ( empty ( $ assoc_args ['tags_input ' ] ) ) {
199+ $ post_arr ['tags_input ' ] = $ this ->get_tags ( $ post_id );
200+ }
201+ $ assoc_args = array_merge ( $ post_arr , $ assoc_args );
202+ }
203+
176204 $ assoc_args = wp_slash ( $ assoc_args );
177205 parent ::_create ( $ args , $ assoc_args , function ( $ params ) {
178206 return wp_insert_post ( $ params , true );
@@ -797,4 +825,60 @@ private function get_category_ids( $arg ) {
797825 // If no category ids found, return exploded array for compat with previous WP-CLI versions.
798826 return $ category_ids ? $ category_ids : $ categories ;
799827 }
828+
829+ /**
830+ * Get post metadata.
831+ *
832+ * @param $post_id ID of the post.
833+ *
834+ * @return array
835+ */
836+ private function get_metadata ( $ post_id ) {
837+ $ metadata = get_metadata ( 'post ' , $ post_id );
838+ $ items = array ();
839+ foreach ( $ metadata as $ key => $ values ) {
840+ foreach ( $ values as $ item_value ) {
841+ $ item_value = maybe_unserialize ( $ item_value );
842+ $ items [$ key ] = $ item_value ;
843+ }
844+ }
845+
846+ return $ items ;
847+ }
848+
849+ /**
850+ * Get Categories of a post.
851+ *
852+ * @param $post_id ID of the post.
853+ *
854+ * @return array
855+ */
856+ private function get_category ( $ post_id ) {
857+ $ category_data = get_the_category ( $ post_id );
858+ $ category_arr = array ();
859+ foreach ( $ category_data as $ cat ) {
860+ array_push ( $ category_arr , $ cat ->term_id );
861+ }
862+
863+ return $ category_arr ;
864+ }
865+
866+ /**
867+ * Get Tags of a post.
868+ *
869+ * @param $post_id ID of the post.
870+ *
871+ * @return array
872+ */
873+ private function get_tags ( $ post_id ) {
874+ $ tag_data = get_the_tags ( $ post_id );
875+ $ tag_arr = array ();
876+ if ( $ tag_data ) {
877+ foreach ( $ tag_data as $ tag ) {
878+ array_push ( $ tag_arr , $ tag ->slug );
879+ }
880+ }
881+
882+ return $ tag_arr ;
883+ }
800884}
0 commit comments