@@ -77,10 +77,10 @@ public function __construct() {
7777 *
7878 * [--post_name=<post_name>]
7979 * : The post name. Default is the sanitized post title when creating a new post.
80- *
81- * [--from-post=<post_id>]
82- * : The post id of a post to be duplicated.
83- *
80+ *
81+ * [--from-post=<post_id>]
82+ * : The post id of a post to be duplicated.
83+ *
8484 * [--to_ping=<to_ping>]
8585 * : Space or carriage return-separated list of URLs to ping. Default empty.
8686 *
@@ -150,11 +150,11 @@ public function __construct() {
150150 * # Create a post with multiple meta values.
151151 * $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"}
152152 * Success: Created post 1923.
153- *
154- * # Create a duplicate post with same post data.
155- * $ wp post create --from-post=123 --post_title='Different Title'
156- * Success: Created post 2350.
157- */
153+ *
154+ * # Create a duplicate post with same post data.
155+ * $ wp post create --from-post=123 --post_title='Different Title'
156+ * Success: Created post 2350.
157+ */
158158 public function create ( $ args , $ assoc_args ) {
159159 if ( ! empty ( $ args [0 ] ) ) {
160160 $ assoc_args ['post_content ' ] = $ this ->read_from_file_or_stdin ( $ args [0 ] );
@@ -181,19 +181,25 @@ public function create( $args, $assoc_args ) {
181181 $ assoc_args = \WP_CLI \Utils \parse_shell_arrays ( $ assoc_args , $ array_arguments );
182182
183183 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-
191- if ( empty ( $ assoc_args ['meta_input ' ] ) ) {
192- $ assoc_args ['meta_input ' ] = $ this ->get_metadata ( $ post_id );
193- }
194-
195- $ assoc_args = array_merge ( $ post_arr , $ assoc_args );
196- }
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+ }
197203
198204 $ assoc_args = wp_slash ( $ assoc_args );
199205 parent ::_create ( $ args , $ assoc_args , function ( $ params ) {
@@ -820,21 +826,59 @@ private function get_category_ids( $arg ) {
820826 return $ category_ids ? $ category_ids : $ categories ;
821827 }
822828
823- /**
824- * Get post metadata.
825- *
826- * @param $post_id id of the post.
827- * @return array
828- */
829- private function get_metadata ( $ post_id = null ) {
830- $ metadata = get_metadata ( 'post ' , $ post_id );
831- $ items = array ();
832- foreach ( $ metadata as $ key => $ values ) {
833- foreach ( $ values as $ item_value ) {
834- $ item_value = maybe_unserialize ( $ item_value );
835- $ items [ $ key ] = $ item_value ;
836- }
837- }
838- return $ items ;
839- }
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 postid 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 postid 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+ }
840884}
0 commit comments