WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 7 years ago

Last modified 4 years ago

#23506 closed defect (bug) (fixed)

get_terms() assumes its taxonomies argument is a numeric array with a 0 key

Reported by: nbachiyski Owned by: wonderboymusic
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.6
Component: Taxonomy Keywords: has-patch commit
Focuses: Cc:

Description

In a few places in get_terms() we are using $taxonomies[0] after we've established that we have more than one taxonomy. The array isn't always 0-based, though.

For example, get_taxonomies() returns arrays with the taxonomy name as a key, which breaks this pattern.

In the attached patch, the taxonomies are run through array_values(), which gives us a 0-based array of the taxonomies.

Attachments (3)

get-terms-0-based-taxonomies.diff (543 bytes) - added by nbachiyski 8 years ago.
23506.diff (2.1 KB) - added by webord 8 years ago.
Using reset to get the first Item of the array
23506.2.diff (2.3 KB) - added by wonderboymusic 7 years ago.

Download all attachments as: .zip

Change History (14)

#2 @Viper007Bond
8 years ago

  • Summary changed from get_terms() assumes its taxonomies argument is 0-based to get_terms() assumes its taxonomies argument is a numeric array

Fixing ticket title to clearer terminology. Probably worth updating the patch too.

#3 follow-up: @nbachiyski
8 years ago

I still think 0-based is the better term.

The array, which is a result of $a = array( 1, 2, 3); unset( $a[0] ); is still numeric, but its zeroth element is missing.

#4 in reply to: ↑ 3 @Viper007Bond
8 years ago

  • Summary changed from get_terms() assumes its taxonomies argument is a numeric array to get_terms() assumes its taxonomies argument is a numeric array with a 0 key

Replying to nbachiyski:

I still think 0-based is the better term.

The array, which is a result of $a = array( 1, 2, 3); unset( $a[0] ); is still numeric, but its zeroth element is missing.

True!

#5 @markjaquith
8 years ago

  • Milestone changed from Awaiting Review to Future Release

Seems prudent. Alternatively (or even additionally), we could start using using the proper array function to grab the first item, instead of [0] (i.e. current(), or even list()).

@webord
8 years ago

Using reset to get the first Item of the array

#6 @webord
8 years ago

  • Cc bordoni.dev@… added
  • Keywords dev-feedback added

#7 @wonderboymusic
7 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Future Release to 3.7

+ whitespace in the refresh

#8 @wonderboymusic
7 years ago

  • Keywords commit added

this passes all unit tests

#9 @nacin
7 years ago

Since we're adjusting whitespace, $hierarchy[$child_of] should be $hierarchy[ $child_of ] — for some time we have put spaces here when the key is a variable.

#10 @wonderboymusic
7 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 25108:

Use reset() to grab the first taxonomy in an array, not by numerical index. Cleans up whitespace. Props webord, nbachiyski. Fixes #23506.

#11 @boonebgorges
4 years ago

In 40924:

When querying for terms, do not assume that $taxonomies is a 0-indexed array.

In [25108], the logic of term queries was modified to avoid assuming
that the taxonomies array was numerically indexed. See #23506. This
fix was inadvertantly reverted during the refactor in [25162].

Props david.binda.
Fixes #41113.

Note: See TracTickets for help on using tickets.