Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
20 changes: 20 additions & 0 deletions features/post.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
18 changes: 16 additions & 2 deletions src/Post_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function __construct() {
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
*
* [--meta_input=<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.
*
* [<file>]
* : Read post content from <file>. If this value is present, the
Expand Down Expand Up @@ -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"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ' is not balanced here.

--meta_input='{"key1":"value1","key2":"value2"}'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed a while ago in #147.

* Success: Created post 1923.
*/
public function create( $args, $assoc_args ) {
if ( ! empty( $args[0] ) ) {
Expand All @@ -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 );
Expand Down Expand Up @@ -249,7 +256,7 @@ public function create( $args, $assoc_args ) {
* : Array of taxonomy terms keyed by their taxonomy name. Default empty.
*
* [--meta_input=<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.
*
* [<file>]
* : Read post content from <file>. If this value is present, the
Expand All @@ -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"}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ' is not balanced here.

it should be :
--meta_input='{"key1":"value1","key2":"value2"}'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

* Success: Updated post 123.
*/
public function update( $args, $assoc_args ) {

Expand All @@ -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 );
Expand Down