[wp-trac] [WordPress Trac] #31316: wp_list_pluck unexpectedly returns id indexed array instead of plucked values with index_key = null

WordPress Trac noreply at wordpress.org
Fri Feb 13 01:35:15 UTC 2015


#31316: wp_list_pluck unexpectedly returns id indexed array instead of plucked
values with index_key = null
-----------------------------+-----------------------------
 Reporter:  adamsilverstein  |      Owner:
     Type:  defect (bug)     |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  General          |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 Recently cane across an interesting bug. We were grabbing a list of term
 slugs, using the following code:
 {{{
 $term_slugs = wp_list_pluck( get_the_terms( $ID, 'category' ), 'slug' );
 }}}

 As a result of a [https://core.trac.wordpress.org/ticket/31086 recently
 patched bug], get_the_terms' cached return was an array that used term ID
 as the index for each term object, instead of the usual 'zero-indexed' (or
 sequential) array.

 Interestingly, when this indexed array was passed to wp_list_pluck, the
 return was not the expected array of values. Instead, wp_list_pluck
 returned an indexed array of values, what you would expect if the function
 had been called with $index_key = 'term_id'.

 wp_list_pluck should "Pluck a certain field out of each object in a list."
 and shouldn't care if the list is indexed; we should always expect an
 array of the found values unless the $index_key option is set (matching
 the behaviour of PHP's `array_column`).

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31316>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list