WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 3 years ago

#11003 new defect (bug)

wp_get_object_terms Returns Duplicate Terms

Reported by: filosofo Owned by: ryan
Milestone: Future Release Priority: normal
Severity: normal Version: 2.9
Component: Taxonomy Keywords: has-patch dev-feedback
Focuses: Cc:

Description

wp_get_object_terms() accepts multiple objects in its first parameter. If the multiple objects have terms in common, then wp_get_object_terms() returns those terms more than once in the returned array of term data.

Patch makes the array of terms unique.

Attachments (4)

wp_get_object_terms-unique-results.11003.diff (859 bytes) - added by filosofo 5 years ago.
11003.diff (1.7 KB) - added by dd32 4 years ago.
11003.patch (2.4 KB) - added by hakre 4 years ago.
11003.notice.patch (494 bytes) - added by scribu 4 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 dd325 years ago

Any chance of this in for 2.9?

comment:2 dd324 years ago

By default, array_unique() compares the contents of the array as a string, fouls it up as you found out..

Did setting the compare to REGULAR help at all? ie. $terms = array_unique($terms, SORT_REGULAR);

comment:3 dd324 years ago

The attached patch has a slight issue, {{fields = all_with_object_id}}} will no longer return a the full set of object Id/term id/taxonomy id suite, only the first termid found for each..

Simply adding DISTINCT to the queries seems a better option to me.. More foolproof to let the SQL layer filter out the other rows. see patch.

dd324 years ago

comment:4 dd324 years ago

  • Keywords tested commit removed

I've tested the patch very basic, And seems to work in all the cases i can think of.

comment:5 dd324 years ago

Before:

$terms = wp_get_object_terms( array(10,12), 'category', array('fields' => 'all_with_object_id') );

var_dump($terms);

array(2) {
  [0]=>
  object(stdClass)#10 (10) {
    ["term_id"]=>
    string(1) "4"
    ["name"]=>
    string(6) "Artist"
    ["slug"]=>
    string(6) "artist"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "4"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "2"
    ["object_id"]=>
    string(2) "10"
  }
  [1]=>
  object(stdClass)#9 (10) {
    ["term_id"]=>
    string(1) "4"
    ["name"]=>
    string(6) "Artist"
    ["slug"]=>
    string(6) "artist"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "4"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "2"
    ["object_id"]=>
    string(2) "12"
  }
}

Note, Object ID in there.

With wp_get_object_terms-unique-results.11003.diff:

$terms = wp_get_object_terms( array(10,12), 'category', array('fields' => 'all_with_object_id') );

var_dump($terms);
 
array(1) {
  [4]=>
  object(stdClass)#9 (10) {
    ["term_id"]=>
    string(1) "4"
    ["name"]=>
    string(6) "Artist"
    ["slug"]=>
    string(6) "artist"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "4"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "2"
    ["object_id"]=>
    string(2) "12"
  }
}

Note, that due to the indexing of the array on $term->term_id in order to filter out duplicates, the 2 have been combined.

hakre4 years ago

comment:6 hakre4 years ago

  • Keywords dev-feedback added

It might make sense to have that optional to not change the current behaviour (back-compat), for example I might want to have that dupes instead of not.

In my patch I adopted the premier suggestion and added an additional parameter: select. It might make sense to adopt that to the SQL queries as well.

comment:7 ryan4 years ago

  • Milestone changed from 3.0 to 3.1

comment:8 kevinB4 years ago

  • Cc kevinB added

comment:9 scribu4 years ago

  • Cc scribu added
  • Keywords changed from has-patch, dev-feedback to has-patch dev-feedback

comment:10 scribu4 years ago

It's pretty easy to make the terms unique afterwards, once you know there might be duplicates.

Maybe we should just add a notice in the documentation?

scribu4 years ago

comment:11 filosofo4 years ago

It's better than nothing, but not a true solution.

comment:12 scribu4 years ago

How about this: use DISTINCT to remove duplicate terms, but only when $fields != 'all_with_object_id'.

comment:13 nacin3 years ago

  • Milestone changed from Awaiting Triage to Future Release

comment:14 mikeschinkel3 years ago

  • Cc mikeschinkel@… added

Related superset of this ticket: ##15675

Note: See TracTickets for help on using tickets.