WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 9 months ago

#21228 closed enhancement (fixed)

Add term_taxonomy_id to WP_Tax_Query fields

Reported by: wonderboymusic Owned by: nacin
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.4.1
Component: Taxonomy Keywords: has-patch needs-testing commit
Focuses: Cc:

Description

Allows WP_Tax_Query::transform_query( ) to not make any queries in some cases - this already works in the following cases:

1) tax_query with 'include_children' => false, for hierarchical and non-hierarchical taxonomies
2) non-hierarchical taxonomies

By adding the condition in my patch to WP_Tax_Query::transform_query( ), 'include_children' => true works for hierarchical taxonomies when 'field' is 'term_taxonomy_id.'

If cases 1 or 2 are true, the query doesn't need to be transformed at all, which is better than resolving term_ids or slugs / names to tt_ids

Example query:

add_filter( 'pre_get_posts', function ( $query ) {
    if ( !$query->is_main_query() )
        return $query;

    $tax_query = array(
        'include_children'  => true,
        'taxonomy'          => 'region',
        'field'             => 'term_taxonomy_id',
        'terms'             => array( 7, 8 ),
        'operator'          => 'IN'
    );

    $query->set( 'tax_query', array( $tax_query ) );

    return $query;
} );

register_taxonomy( 'region', 'post', array(
    'hierarchical'      => true,
    'public'            => true,
    'show_ui'           => true,
    'labels'            => array( 'name' => 'Regions' )
) );

Attachments (1)

query-by-tt_id.patch (804 bytes) - added by wonderboymusic 3 years ago.

Download all attachments as: .zip

Change History (4)

comment:1 @nacin3 years ago

  • Keywords needs-testing commit added
  • Milestone changed from Awaiting Review to 3.5

+100

comment:2 @nacin3 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [21552]:

Add term_taxonomy_id field handling to WP_Tax_Query::transform_query(), allowing the transformation to be bypassed when tt ids are passed.

props wonderboymusic.
fixes #21228.

comment:3 @ircbot9 months ago

This ticket was mentioned in IRC in #wordpress-dev by helen. View the logs.

Note: See TracTickets for help on using tickets.