Make WordPress Core

Changeset 31366


Ignore:
Timestamp:
02/07/2015 07:49:17 PM (9 years ago)
Author:
boonebgorges
Message:

In WP_Query::get_queried_object(), avoid PHP notices when is_tax is paired with an empty tax_query.

It's possible to have an empty tax_query and is_tax=true when the initial
query contains a taxonomy var (and is processed as such during
WP_Query::parse_query()) but the taxonomy var is unset during a 'parse_query'
callback. While this kind of behavior is not necessarily something we need to
support, we should continue to avoid PHP notices in such cases, as we did prior
to WP 4.1.

Fixes #31246.

Location:
trunk
Files:
2 edited

Legend:

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

    r31340 r31366  
    39013901                $tax_query_in_and = wp_list_filter( $this->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' );
    39023902
    3903                 $queried_taxonomies = array_keys( $tax_query_in_and );
    3904                 $matched_taxonomy = reset( $queried_taxonomies );
    3905                 $query = $tax_query_in_and[ $matched_taxonomy ];
    3906 
    3907                 if ( $query['terms'] ) {
    3908                     if ( 'term_id' == $query['field'] ) {
    3909                         $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
    3910                     } else {
    3911                         $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
     3903                if ( ! empty( $tax_query_in_and ) ) {
     3904                    $queried_taxonomies = array_keys( $tax_query_in_and );
     3905                    $matched_taxonomy = reset( $queried_taxonomies );
     3906                    $query = $tax_query_in_and[ $matched_taxonomy ];
     3907
     3908                    if ( $query['terms'] ) {
     3909                        if ( 'term_id' == $query['field'] ) {
     3910                            $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
     3911                        } else {
     3912                            $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
     3913                        }
    39123914                    }
    39133915                }
  • trunk/tests/phpunit/tests/query.php

    r30085 r31366  
    9292        $this->assertEquals( $query->get_queried_object(), $tag );
    9393    }
     94
     95    /**
     96     * @ticket 31246
     97     */
     98    public function test_get_queried_object_should_return_null_when_is_tax_is_true_but_the_taxonomy_args_have_been_removed_in_a_parse_query_callback() {
     99        // Don't override the args provided below.
     100        remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
     101        register_taxonomy( 'wptests_tax', 'post' );
     102        $terms = $this->factory->term->create_many( 2, array(
     103            'taxonomy' => 'wptests_tax',
     104        ) );
     105
     106        $posts = $this->factory->post->create_many( 2 );
     107
     108        wp_set_object_terms( $posts[0], array( $terms[0] ), 'wptests_tax' );
     109        wp_set_object_terms( $posts[1], array( $terms[1] ), 'wptests_tax' );
     110
     111        add_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
     112        $q = new WP_Query( array(
     113            'fields' => 'ids',
     114            'wptests_tax' => $terms[1],
     115        ) );
     116
     117        remove_action( 'parse_query', array( $this, 'filter_parse_query_to_remove_tax' ) );
     118
     119        $this->assertNull( $q->get_queried_object() );
     120    }
     121
     122    public function filter_parse_query_to_remove_tax( $q ) {
     123        unset( $q->query_vars['wptests_tax'] );
     124    }
    94125}
Note: See TracChangeset for help on using the changeset viewer.