Make WordPress Core

Changeset 27511


Ignore:
Timestamp:
03/12/2014 04:07:41 AM (11 years ago)
Author:
wonderboymusic
Message:

In WP_Query::get_queried_object(), account for pre_get_posts by checking for tag when tag_id isn't present. Tags still need to be rolled up into tax_query. Add a unit test confirming expected query vars during and after pre_get_posts.

Props mattonomics for a patch.
See #27362.

Location:
trunk
Files:
2 edited

Legend:

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

    r27456 r27511  
    36643664                }
    36653665            } elseif ( $this->is_tag ) {
    3666                 $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
     3666                if ( $this->get( 'tag_id' ) ) {
     3667                    $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
     3668                } elseif ( $this->get( 'tag' ) ) {
     3669                    $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
     3670                }
    36673671            } else {
    36683672                $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
  • trunk/tests/phpunit/tests/query.php

    r27456 r27511  
    105105        $query->set( 'posts_per_rss', 30 );
    106106    }
     107
     108    /**
     109     * @ticket 26627
     110     */
     111    function test_tag_queried_object() {
     112        $slug = 'tag-slug-26627';
     113        $this->factory->tag->create( array( 'slug' => $slug ) );
     114        $tag = get_term_by( 'slug', $slug, 'post_tag' );
     115
     116        add_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 );
     117
     118        $this->go_to( get_term_link( $tag ) );
     119
     120        $this->assertQueryTrue( 'is_tag', 'is_archive' );
     121        $this->assertNotEmpty( get_query_var( 'tag_id' ) );
     122        $this->assertNotEmpty( get_query_var( 'tag' ) );
     123        $this->assertEmpty( get_query_var( 'tax_query' ) );
     124        $this->assertCount( 1, get_query_var( 'tag_slug__in' ) );
     125        $this->assertEquals( get_queried_object(), $tag );
     126
     127        remove_action( 'pre_get_posts', array( $this, '_tag_queried_object' ), 11 );
     128    }
     129
     130    function _tag_queried_object( &$query ) {
     131        $tag = get_term_by( 'slug', 'tag-slug-26627', 'post_tag' );
     132        $this->assertTrue( $query->is_tag() );
     133        $this->assertTrue( $query->is_archive() );
     134        $this->assertNotEmpty( $query->get( 'tag' ) );
     135        $this->assertCount( 1, $query->get( 'tag_slug__in' ) );
     136        $this->assertEquals( $query->get_queried_object(), $tag );
     137    }
    107138}
Note: See TracChangeset for help on using the changeset viewer.