Skip to content

Commit 6ec0aab

Browse files
authored
Merge pull request #523 from BhargavBhandari90/issue-325
2 parents f349e8f + 97d52fa commit 6ec0aab

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

features/post-meta.feature

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,39 @@ Feature: Manage post custom fields
219219
My\New\Meta
220220
"""
221221

222+
Scenario: List post meta with or without single flag
223+
Given a WP install
224+
225+
When I run `wp post meta add 1 apple banana`
226+
And I run `wp post meta add 1 apple mango`
227+
Then STDOUT should not be empty
228+
229+
When I run `wp post meta get 1 apple`
230+
Then STDOUT should be:
231+
"""
232+
banana
233+
"""
234+
235+
When I run `wp post meta get 1 apple --single`
236+
Then STDOUT should be:
237+
"""
238+
banana
239+
"""
240+
241+
When I run `wp post meta get 1 apple --no-single`
242+
Then STDOUT should be:
243+
"""
244+
array (
245+
0 => 'banana',
246+
1 => 'mango',
247+
)
248+
"""
249+
When I run `wp post meta get 1 apple --no-single --format=json`
250+
Then STDOUT should be:
251+
"""
252+
["banana","mango"]
253+
"""
254+
222255
@pluck
223256
Scenario: Nested values can be retrieved.
224257
Given a WP install

src/WP_CLI/CommandWithMeta.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ function ( $a, $b ) use ( $orderby, $order ) {
142142
* <key>
143143
* : The name of the meta field to get.
144144
*
145+
* [--single]
146+
* : Whether to return a single value.
147+
*
145148
* [--format=<format>]
146149
* : Get value in a particular format.
147150
* ---
@@ -156,8 +159,9 @@ public function get( $args, $assoc_args ) {
156159
list( $object_id, $meta_key ) = $args;
157160

158161
$object_id = $this->check_object_id( $object_id );
162+
$single = Utils\get_flag_value( $assoc_args, 'single', true );
159163

160-
$value = $this->get_metadata( $object_id, $meta_key, true );
164+
$value = $this->get_metadata( $object_id, $meta_key, $single );
161165

162166
if ( '' === $value ) {
163167
die( 1 );

0 commit comments

Comments
 (0)