diff --git a/features/post-meta.feature b/features/post-meta.feature index b75bd3d2c..237f99a24 100644 --- a/features/post-meta.feature +++ b/features/post-meta.feature @@ -219,6 +219,39 @@ Feature: Manage post custom fields My\New\Meta """ +Scenario: List post meta with or without single flag + Given a WP install + + When I run `wp post meta add 1 apple banana` + And I run `wp post meta add 1 apple mango` + Then STDOUT should not be empty + + When I run `wp post meta get 1 apple` + Then STDOUT should be: + """ + banana + """ + + When I run `wp post meta get 1 apple --single` + Then STDOUT should be: + """ + banana + """ + + When I run `wp post meta get 1 apple --no-single` + Then STDOUT should be: + """ + array ( + 0 => 'banana', + 1 => 'mango', + ) + """ + When I run `wp post meta get 1 apple --no-single --format=json` + Then STDOUT should be: + """ + ["banana","mango"] + """ + @pluck Scenario: Nested values can be retrieved. Given a WP install diff --git a/src/WP_CLI/CommandWithMeta.php b/src/WP_CLI/CommandWithMeta.php index bee25719d..984f66c14 100644 --- a/src/WP_CLI/CommandWithMeta.php +++ b/src/WP_CLI/CommandWithMeta.php @@ -142,6 +142,9 @@ function ( $a, $b ) use ( $orderby, $order ) { * * : The name of the meta field to get. * + * [--single] + * : Whether to return a single value. + * * [--format=] * : Get value in a particular format. * --- @@ -156,8 +159,9 @@ public function get( $args, $assoc_args ) { list( $object_id, $meta_key ) = $args; $object_id = $this->check_object_id( $object_id ); + $single = Utils\get_flag_value( $assoc_args, 'single', true ); - $value = $this->get_metadata( $object_id, $meta_key, true ); + $value = $this->get_metadata( $object_id, $meta_key, $single ); if ( '' === $value ) { die( 1 );