[wp-trac] [WordPress Trac] #41265: Introduce a Term Meta queries: Extension of WP_Query (tax_query)

WordPress Trac noreply at wordpress.org
Fri Jul 7 12:28:29 UTC 2017


#41265: Introduce a Term Meta queries: Extension of WP_Query (tax_query)
-----------------------------+-----------------------------
 Reporter:  Blackbam         |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  General          |    Version:
 Severity:  normal           |   Keywords:
  Focuses:                   |
-----------------------------+-----------------------------
 In WP_Query we have a possibility to query posts depending on their meta
 value. Look at the following example:


 {{{
 $args = array(
         'post_type'  => 'product',
         'meta_query' => array(
                 array(
                         'key'     => 'color',
                         'value'   => 'blue',
                         'compare' => 'NOT LIKE',
                 ),
         ),
 );
 $query = new WP_Query( $args );
 }}}


 We also have a possibility to query by terms (custom taxonomy). Look at
 the following example:


 {{{
 $args = array(
         'post_type' => 'post',
         'tax_query' => array(
                 array(
                         'taxonomy' => 'people',
                         'field'    => 'slug',
                         'terms'    => 'bob',
                 ),
         ),
 );
 $query = new WP_Query( $args );
 }}}


 In WordPress '''4.4.0''' we have introduced two important functions for
 developers. We can add and remove meta data to terms. Have a look at the
 following functions:

 https://developer.wordpress.org/reference/functions/get_term_meta/
 https://developer.wordpress.org/reference/functions/update_term_meta/
 https://developer.wordpress.org/reference/functions/delete_term_meta/

 This introduction was great. However there is still NO WAY to use term
 meta within WP_Query. In my understanding '''a term meta query should be
 an extension of the tax_query'''.

 Currently a tax_query allows for the following parameters:

  - taxonomy
  - field
  - terms
  - include_children
  - operator


 More information here:
 https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters


 '''We should add a meta_query functionality to the tax_query'''.

 My suggestion is to do it like this: For tax_query we allow the same
 parameters as for meta_query.

 https://codex.wordpress.org/Class_Reference/WP_Meta_Query

  - (tax_)meta_key
  - (tax_)meta_value
  - (tax_)meta_type
  - (tax_)meta_compare
  - (tax_)meta_query

 Example use case of what it could look like (renaming the parameters to
 (tax_) is maybe not needed):


 {{{
 $args = array(
         'post_type' => 'post',
         'tax_query' => array(
                 array(
                         'taxonomy' => 'people',
                         'tax_meta_query' => array(
                             'key'     => 'color',
                             'value'   => 'blue',
                             'compare' => 'NOT LIKE',
                         )
                 ),
         ),
 );
 $query = new WP_Query( $args );
 }}}

 For the best exact implementation my knowledge of the WordPress core is
 not good enough however I think this feature will be needed!

 Here you can also see a use case of what I mean:
 https://wordpress.stackexchange.com/questions/272585/wp-query-tax-query-
 and-term-meta-how-to/272589

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


More information about the wp-trac mailing list