#11003 closed defect (bug) (fixed)
wp_get_object_terms Returns Duplicate Terms
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 4.0 | Priority: | normal |
| Severity: | normal | Version: | 2.9 |
| Component: | Taxonomy | Keywords: | has-patch |
| 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)
Change History (21)
#2
@
16 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);
#3
@
16 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.
#4
@
16 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.
#5
@
16 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.
#6
@
16 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.
#9
@
16 years ago
- Cc scribu added
- Keywords changed from has-patch, dev-feedback to has-patch dev-feedback
#10
@
16 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?
#12
@
16 years ago
How about this: use DISTINCT to remove duplicate terms, but only when $fields != 'all_with_object_id'.
#15
@
12 years ago
- Keywords dev-feedback removed
- Milestone changed from Future Release to 4.0
Since we are trying to filter by uniqueness, the dupes that have different values for object_id are not really dupes, and they only appear when 'all_with_object_id' is passed.
$terms = array_values( array_unique( $terms, SORT_REGULAR ) ); pre-filter works for both solutions described above.
Any chance of this in for 2.9?