[wp-trac] [WordPress Trac] #35137: get_terms() with a meta_query filter returns duplicated terms

WordPress Trac noreply at wordpress.org
Thu Dec 17 16:39:02 UTC 2015


#35137: get_terms() with a meta_query filter returns duplicated terms
--------------------------+-----------------------------
 Reporter:  jadpm         |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 `get_terms()` got support for `meta_query` in 4.4. However, it seems that
 under some `meta_query` scenarios it returns duplicated results - for
 example, when the same term has more than one value for a termmeta key.

 Consider the following `termmeta` table, asuming the item with `term_id =
 1` is the default category:

 ||= meta_id =||||= term_id =||||= meta_key =||||= meta_value =||
 || 1 |||| 1 |||| foo |||| bar ||
 || 2 |||| 1 |||| foo |||| bor ||

 Now, consider this arguments:
 {{{
 Array
 (
     [hide_empty] => 1
     [hierarchical] => 1
     [pad_counts] => 1
     [orderby] => name
     [order] => DESC
     [meta_query] => Array
         (
             [0] => Array
                 (
                     [key] => foo
                     [value] => bur
                     [type] => CHAR
                     [compare] => !=
                 )

             [relation] => AND
         )

 )
 }}}

 The performed query in `get_terms()` is as follows:
 {{{
 SELECT t.*, tt.*
 FROM wp_dev_terms AS t
 INNER JOIN wp_dev_termmeta ON ( t.term_id = wp_dev_termmeta.term_id )
 INNER JOIN wp_dev_term_taxonomy AS tt ON t.term_id = tt.term_id
 WHERE tt.taxonomy IN ('category')
 AND (
   ( wp_dev_termmeta.meta_key = 'foo' AND CAST(wp_dev_termmeta.meta_value
 AS CHAR) != 'bur' )
 )
 ORDER BY t.name DESC
 }}}

 Note that as there is no `DISTINCT` or `GROUP BY t.term_id` statement, we
 get the same row twice, as both rows meet the query requirements.

 When querying posts, as far as I've seen, we do group by post `ID`. We
 might want to do the same here.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/35137>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list