WP_CLI
Formatter::show_single_field
Show a single field from a list of items.
Method of the class: Formatter{}
No Hooks.
Returns
null. Nothing (null).
Usage
// private - for code of main (parent) class only $result = $this->show_single_field( $items, $field );
- $items(array) (required)
- Array of objects to show fields from.
- $field(string) (required)
- The field to show.
Formatter::show_single_field() Formatter::show single field code WP-CLI 2.13.0-alpha
private function show_single_field( $items, $field ) {
$key = null;
$values = [];
foreach ( $items as $item ) {
$item = (object) $item;
if ( null === $key ) {
$key = $this->find_item_key( $item, $field );
}
if ( 'json' === $this->args['format'] ) {
$values[] = $item->$key;
} else {
WP_CLI::print_value(
$item->$key,
[
'format' => $this->args['format'],
]
);
}
}
if ( 'json' === $this->args['format'] ) {
echo json_encode( $values );
}
}