Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#21608 closed defect (bug) (invalid)

get_the_terms() returns an array with keys that are not starting from 0

Reported by: boryanka's profile boryanka Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

This is easy to reproduce. You need a custom post type (in my case 'events') and a custom taxonomy (in my case 'eventtype').

When I try to retrieve the names of the eventtypes associated with the current event it gives back an array. The issue is that the key of each array line is the ID of the term. Instead of starting from 0.

The code:

$terms = get_the_terms( $post->ID, 'eventtype' );

The output:

array(1) {
  [6]=>
  object(stdClass)#249 (10) {
    ["term_id"]=>
    string(1) "6"
    ["name"]=>
    string(5) "Event"
    ["slug"]=>
    string(5) "event"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "6"
    ["taxonomy"]=>
    string(9) "eventtype"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(3) "182"
  }
}

Change History (9)

#1 @markoheijnen
12 years ago

As far as I can tell that is default behaviour. As you can see the number is the term_id

#2 @boryanka
12 years ago

Yes, maybe it is, but what would you do if you had let's say 5 terms for this post and you only wanted to list the first or last one?

There are ways to do it, of course - workarounds.

#3 @scribu
12 years ago

I think the workaround is pretty easy:

$terms = get_the_terms( ... );
$first_term = reset( $terms );
$last_term = end( $terms );
Last edited 12 years ago by scribu (previous) (diff)

#4 follow-up: @JustinSainton
12 years ago

Or use array_values(). Surely the benefits of having the $term_id listed far outweigh the issues of indexing you're describing. Not only that, but changing the behavior would undoubtedly break countless plugins that rely on the current behavior.

Last edited 12 years ago by JustinSainton (previous) (diff)

#5 in reply to: ↑ 4 @nacin
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Replying to JustinSainton:

Or use array_values(). Surely the benefits of having the $term_id listed far outweigh the issues of indexing you're describing. Not only that, but changing the behavior would undoubtedly break countless plugins that rely on the current behavior.

Indeed.

#6 @SergeyBiryukov
11 years ago

#24562 was marked as a duplicate.

#7 @SergeyBiryukov
11 years ago

  • Component changed from General to Taxonomy

#8 @leedo
11 years ago

Perhaps the docs should be updated to reflect this?

#9 @dcavins
11 years ago

I noticed the behavior mentioned in this ticket, but only on the front end. When I use get_the_terms in the WP admin area, the array looks like this:

Array
(
    [0] => stdClass Object
        (
            [term_id] => 1660
            [name] => Bonner County
            [slug] => bonner-county-idaho
            [term_group] => 0
            [term_taxonomy_id] => 1755
            [taxonomy] => geographies
            [description] => 05000US16017
            [parent] => 841
            [count] => 0
        )
)

I don't know if there are disadvantages, but wp_get_object_terms returns an array with keys of 0, 1, and so on, in both the front end and admin area, if that's the preferred behavior.

Last edited 11 years ago by dcavins (previous) (diff)
Note: See TracTickets for help on using tickets.