Opened 4 years ago
Last modified 2 years ago
#11003 new defect (bug)
wp_get_object_terms Returns Duplicate Terms
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Taxonomy | Version: | 2.9 |
| Severity: | normal | Keywords: | has-patch dev-feedback |
| Cc: | kevinB, scribu, mikeschinkel@… |
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)
Change History (18)
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);
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.
- Keywords tested commit removed
I've tested the patch very basic, And seems to work in all the cases i can think of.
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.
- 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.
- Cc scribu added
- Keywords changed from has-patch, dev-feedback to has-patch dev-feedback
comment:10
scribu — 3 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?
comment:11
filosofo — 3 years ago
It's better than nothing, but not a true solution.
comment:12
scribu — 3 years ago
How about this: use DISTINCT to remove duplicate terms, but only when $fields != 'all_with_object_id'.
comment:13
nacin — 3 years ago
- Milestone changed from Awaiting Triage to Future Release
comment:14
mikeschinkel — 2 years ago
- Cc mikeschinkel@… added
Related superset of this ticket: ##15675

Any chance of this in for 2.9?