diff --git a/composer.json b/composer.json index 3b639f3ec..8d47e94c7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ "require": {}, "require-dev": { "behat/behat": "~2.5", - "wp-cli/wp-cli": "*", + "wp-cli/wp-cli": "^1.5", "phpunit/phpunit": "^4.8" }, "extra": { diff --git a/features/post.feature b/features/post.feature index ab0bd1013..d7b1d3215 100644 --- a/features/post.feature +++ b/features/post.feature @@ -232,3 +232,23 @@ Feature: Manage WordPress posts """ var isEmailValid = /^\S+@\S+.\S+$/.test(email); """ + + @require-wp-4.4 + Scenario: Creating/updating posts with meta keys + When I run `wp post create --post_title='Test Post' --post_content='Test post content' --meta_input='{"key1":"value1","key2":"value2"}' --porcelain` + Then STDOUT should be a number + And save STDOUT as {POST_ID} + + When I run `wp post meta list {POST_ID} --format=table` + Then STDOUT should be a table containing rows: + | post_id | meta_key | meta_value | + | {POST_ID} | key1 | value1 | + | {POST_ID} | key2 | value2 | + + When I run `wp post update {POST_ID} --meta_input='{"key2":"value2b","key3":"value3"}'` + And I run `wp post meta list {POST_ID} --format=table` + Then STDOUT should be a table containing rows: + | post_id | meta_key | meta_value | + | {POST_ID} | key1 | value1 | + | {POST_ID} | key2 | value2b | + | {POST_ID} | key3 | value3 | diff --git a/src/Post_Command.php b/src/Post_Command.php index 0641190a3..ad2351efc 100644 --- a/src/Post_Command.php +++ b/src/Post_Command.php @@ -112,7 +112,7 @@ public function __construct() { * : Array of taxonomy terms keyed by their taxonomy name. Default empty. * * [--meta_input=] - * : Array of post meta values keyed by their post meta key. Default empty. + * : Array in JSON format of post meta values keyed by their post meta key. Default empty. * * [] * : Read post content from . If this value is present, the @@ -143,6 +143,10 @@ public function __construct() { * # Create post with content from given file * $ wp post create ./post-content.txt --post_category=201,345 --post_title='Post from file' * Success: Created post 1922. + * + * # Create a post with multiple meta values. + * $ wp post create --post_title='A post' --post_content='Just a small post.' --meta_input='{"key1":"value1","key2":"value2"} + * Success: Created post 1923. */ public function create( $args, $assoc_args ) { if ( ! empty( $args[0] ) ) { @@ -162,6 +166,9 @@ public function create( $args, $assoc_args ) { $assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] ); } + $array_arguments = array( 'meta_input' ); + $assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments ); + $assoc_args = wp_slash( $assoc_args ); parent::_create( $args, $assoc_args, function ( $params ) { return wp_insert_post( $params, true ); @@ -249,7 +256,7 @@ public function create( $args, $assoc_args ) { * : Array of taxonomy terms keyed by their taxonomy name. Default empty. * * [--meta_input=] - * : Array of post meta values keyed by their post meta key. Default empty. + * : Array in JSON format of post meta values keyed by their post meta key. Default empty. * * [] * : Read post content from . If this value is present, the @@ -268,6 +275,10 @@ public function create( $args, $assoc_args ) { * * $ wp post update 123 --post_name=something --post_status=draft * Success: Updated post 123. + * + * # Update a post with multiple meta values. + * $ wp post update 123 --meta_input='{"key1":"value1","key2":"value2"} + * Success: Updated post 123. */ public function update( $args, $assoc_args ) { @@ -285,6 +296,9 @@ public function update( $args, $assoc_args ) { $assoc_args['post_category'] = explode( ',', $assoc_args['post_category'] ); } + $array_arguments = array( 'meta_input' ); + $assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments ); + $assoc_args = wp_slash( $assoc_args ); parent::_update( $args, $assoc_args, function ( $params ) { return wp_update_post( $params, true );