Skip to content
Open
Changes from 3 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
4 changes: 3 additions & 1 deletion src/wp-includes/class-wp-list-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ public function pluck( $field, $index_key = null ) {
*/
foreach ( $this->output as $key => $value ) {
if ( is_object( $value ) ) {
$newlist[ $key ] = $value->$field;
if ( property_exists( $value, $field ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let'e combine the conditions.

if ( is_object( $value ) && property_exists( $value, $field ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure, because it will then fall through to the else case and the user will get a message “Values for the input array must be either objects or arrays.” This will be confusing because the value is indeed an object, but just it doesn't have the property.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@mukeshpanchal27
I was thinking of this solution earlier, but due to the reason which @westonruter mentioned, I had chosen nested if.

$newlist[ $key ] = $value->$field;
}
Comment thread
GautamMKGarg marked this conversation as resolved.
Outdated
} elseif ( is_array( $value ) ) {
$newlist[ $key ] = $value[ $field ];
Comment thread
GautamMKGarg marked this conversation as resolved.
Outdated
} else {
Expand Down
Loading