[wp-trac] [WordPress Trac] #34996: Ordering taxonomies by term meta

WordPress Trac noreply at wordpress.org
Fri Jan 29 11:10:07 UTC 2016


#34996: Ordering taxonomies by term meta
------------------------------------------------------+------------------
 Reporter:  eherman24                                 |       Owner:
     Type:  enhancement                               |      Status:  new
 Priority:  normal                                    |   Milestone:  4.5
Component:  Taxonomy                                  |     Version:  4.4
 Severity:  normal                                    |  Resolution:
 Keywords:  has-patch needs-testing needs-unit-tests  |     Focuses:
------------------------------------------------------+------------------

Comment (by jadpm):

 Adding sorting capabilities only by existing meta_query named-keys has one
 evident problem: you can only sort by a termmeta field that is already on
 the meta_query. Core added support for this on post queries the other way
 around: to provide a cleaner way to sort by a custom field already on a
 meta_query, but keeping sorting capabilities by third meta keys.

 Besides that, the provided patch has sereval problems.

 First, when using at least one meta_query clause `$orderby` is overriden
 to `$args['orderby']` or to an empty string. It tehn gets a value only if
 `$args['orderby']` matches a given meta_query key. Otherwise, it can be
 left empty, rpoducing a broken SQL statement:
 {{{
 SELECT DISTINCT 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_termmeta AS mt1 ON ( t.term_id = mt1.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 = 'field-one'
                 AND CAST(wp_dev_termmeta.meta_value AS CHAR) = '1' )
         AND
                 ( mt1.meta_key = 'field-two'
                 AND CAST(mt1.meta_value AS CHAR) = '1' )
 )
 name ASC
 }}}

 Note the final `name ASC` piece.

 Then, there is a typo on line 1384:
 `$orderby = "ORDER BY CAST(term_meta.meta_value AS term_meta)"; `
 should be
 `$orderby = "ORDER BY CAST(term_meta.meta_value AS
 {$primary_meta_query['type']})"; `

 I also tested whether the suggested patch works with more than one
 meta_query clause, with some interesting results:
 * When adding an `order_by => 'meta_value'` statement, the sorting gets
 applied related to the first meta_query clause.
 * When adding an `'orderby' => 'clause_key'` statement, it does not get
 added at all, since there is no `AND term_meta.meta_key = 'whatever
 meta_query clause key corresponds to that clause_key'`.

 I think this needs a little more work, and a clear guidance. We should
 support both `meta_key`/`orderby="meta_value"` and `orderby="clause_key"`
 formats. I am working on a patch to cover both scenarios.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34996#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list