Make WordPress Core


Ignore:
Timestamp:
11/04/2013 11:53:55 PM (11 years ago)
Author:
wonderboymusic
Message:

Category and tag are typically checked before checking for a custom taxonomy. If the global query matches category or tag (even if it also has tax_query set), return category/tag as the queried object, instead of arbitrarily returning the first term in the tax_query stack (typically those added with 'pre_get_posts').

Real world example: http://www.emusic.com/17dots/topics/daily-download/ - "tag" page, regionalized for US-only content using pre_get_posts passing in the terms "US" and "ALL" for "region" (custom tax). All of the theme functions would output "ALL" as the term name. Even though it was a tag archive, the queried object was an arbitrary term from tax_query.

See [26006]. All unit tests pass.
Fixes #20767.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r25954 r26007  
    32493249
    32503250        if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    3251             $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
    3252 
    3253             $query = reset( $tax_query_in_and );
    3254 
    3255             if ( 'term_id' == $query['field'] )
    3256                 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
    3257             elseif ( $query['terms'] )
    3258                 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3251            if ( $this->is_category ) {
     3252                $term = get_term( $this->get( 'cat' ), 'category' );
     3253            } elseif ( $this->is_tag ) {
     3254                $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
     3255            } else {
     3256                $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
     3257                $query = reset( $tax_query_in_and );
     3258
     3259                if ( 'term_id' == $query['field'] )
     3260                    $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
     3261                else
     3262                    $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3263            }
    32593264
    32603265            if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
     
    32623267                $this->queried_object_id = (int) $term->term_id;
    32633268
    3264                 if ( $this->is_category )
     3269                if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
    32653270                    _make_cat_compat( $this->queried_object );
    32663271            }
Note: See TracChangeset for help on using the changeset viewer.