Make WordPress Core

Changeset 26946 for branches/3.8


Ignore:
Timestamp:
01/15/2014 07:33:23 AM (11 years ago)
Author:
nacin
Message:

Query: Fix category handling in pre_get_posts when pretty permalinks are in use (category_name query variable).

Merges [26864] [26874] [26875] to the 3.8 branch.

props wonderboymusic, SergeyBiryukov.
fixes #26627.

Location:
branches/3.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/3.8

  • branches/3.8/src/wp-includes/query.php

    r26525 r26946  
    32653265        if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    32663266            if ( $this->is_category ) {
    3267                 $term = get_term( $this->get( 'cat' ), 'category' );
     3267                if ( $this->get( 'cat' ) ) {
     3268                    $term = get_term( $this->get( 'cat' ), 'category' );
     3269                } elseif ( $this->get( 'category_name' ) ) {
     3270                    $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
     3271                }
    32683272            } elseif ( $this->is_tag ) {
    32693273                $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
     
    32723276                $query = reset( $tax_query_in_and );
    32733277
    3274                 if ( 'term_id' == $query['field'] )
    3275                     $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
    3276                 else
    3277                     $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3278                if ( $query['terms'] ) {
     3279                    if ( 'term_id' == $query['field'] ) {
     3280                        $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
     3281                    } else {
     3282                        $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3283                    }
     3284                }
    32783285            }
    32793286
  • branches/3.8/tests/phpunit/tests/query/taxQuery.php

    r26007 r26946  
    1818
    1919    protected $cat;
     20    protected $uncat;
    2021    protected $tag;
    2122    protected $tax;
     
    4849        _make_cat_compat( $this->cat );
    4950        $this->tag = get_term( $this->tag_id, 'post_tag' );
     51
     52        $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' );
     53        _make_cat_compat( $this->uncat );
    5054
    5155        add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
     
    107111    }
    108112
     113    function test_cat_uncat_action_tax() {
     114        // category with tax added
     115        add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 );
     116
     117        $this->go_to( home_url( "/category/uncategorized/" ) );
     118        $this->assertQueryTrue( 'is_category', 'is_archive' );
     119        $this->assertNotEmpty( get_query_var( 'cat' ) );
     120        $this->assertNotEmpty( get_query_var( 'tax_query' ) );
     121        $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
     122        $this->assertNotEmpty( get_query_var( 'term_id' ) );
     123        $this->assertEquals( get_queried_object(), $this->uncat );
     124
     125        remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 );
     126    }
     127
     128    function _cat_uncat_action_tax( &$query ) {
     129        $this->assertTrue( $query->is_category() );
     130        $this->assertTrue( $query->is_archive() );
     131        $this->assertNotEmpty( $query->get( 'category_name' ) );
     132        $this->assertNotEmpty( $query->get( 'tax_query' ) );
     133        $this->assertEquals( $query->get_queried_object(), $this->uncat );
     134    }
     135
     136    /**
     137     * @ticket 26728
     138     */
     139    function test_tax_action_tax() {
     140        // tax with tax added
     141        $this->go_to( home_url( '/testtax/tax-slug2/' ) );
     142        $this->assertQueryTrue( 'is_tax', 'is_archive' );
     143        $this->assertNotEmpty( get_query_var( 'tax_query' ) );
     144        $this->assertNotEmpty( get_query_var( 'taxonomy' ) );
     145        $this->assertNotEmpty( get_query_var( 'term_id' ) );
     146        $this->assertEquals( get_queried_object(), get_term( $this->tax_id, 'testtax' ) );
     147    }
     148
    109149    function test_tax_query_tag_action_tax() {
    110150        // tax + tag with tax added
Note: See TracChangeset for help on using the changeset viewer.