WordPress.org

Make WordPress Core

Opened 5 months ago

Closed 4 months ago

#41265 closed enhancement (duplicate)

Extension of WP_Query (tax_query with tax meta)

Reported by: Blackbam Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Query Keywords:
Focuses: Cc:

Description

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

Change History (5)

#1 @Blackbam
5 months ago

  • Summary changed from Introduce a Term Meta queries: Extension of WP_Query (tax_query) to Extension of WP_Query (tax_query)

#2 @Blackbam
5 months ago

  • Summary changed from Extension of WP_Query (tax_query) to Extension of WP_Query (tax_query with tax meta)

#3 @Blackbam
5 months ago

  • Type changed from feature request to enhancement

#4 @Blackbam
5 months ago

  • Component changed from General to Query

#5 @boonebgorges
4 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This appears to be a duplicate of #38308. I'm receptive to something like what's being suggested here. Let's move toward an approach on that ticket.

Note: See TracTickets for help on using tickets.