Make WordPress Core

Changeset 25291


Ignore:
Timestamp:
09/06/2013 10:06:40 PM (11 years ago)
Author:
wonderboymusic
Message:

Ensure that the post type object is the queried object when a post type has been registered with has_archive => true. Ensure it is not stomped when decorated with tax_query. Adds unit tests.

Props nacin.
Fixes #18614.

Location:
trunk
Files:
5 edited

Legend:

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

    r25207 r25291  
    578578    }
    579579
     580    // If there's a post type archive
     581    if ( is_post_type_archive() ) {
     582        $post_type_object = get_post_type_object( get_query_var( 'post_type' ) );
     583        if ( ! $post_type_object->has_archive )
     584            $title = post_type_archive_title( '', false );
     585    }
     586
    580587    // If there's a category or tag
    581588    if ( is_category() || is_tag() ) {
     
    596603    }
    597604
    598     // If there's a post type archive
    599     if ( is_post_type_archive() )
     605    // Post type archives with has_archive should override terms.
     606    if ( is_post_type_archive() && $post_type_object->has_archive )
    600607        $title = post_type_archive_title( '', false );
    601608
     
    697704        return;
    698705
    699     $post_type_obj = get_queried_object();
     706    $post_type_obj = get_post_type_object( get_query_var( 'post_type' ) );
    700707    $title = apply_filters('post_type_archive_title', $post_type_obj->labels->name );
    701708
     
    16711678    $args = wp_parse_args( $args, $defaults );
    16721679
    1673     if ( is_single() || is_page() ) {
     1680    if ( is_singular() ) {
    16741681        $id = 0;
    16751682        $post = get_post( $id );
     
    16791686            $href = get_post_comments_feed_link( $post->ID );
    16801687        }
     1688    } elseif ( is_post_type_archive() ) {
     1689        $post_type_obj = get_post_type_object( get_query_var( 'post_type' ) );
     1690        $title = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name );
     1691        $href = get_post_type_archive_feed_link( $post_type_obj->name );
    16811692    } elseif ( is_category() ) {
    16821693        $term = get_queried_object();
  • trunk/src/wp-includes/query.php

    r25289 r25291  
    31483148     */
    31493149    function is_post_type_archive( $post_types = '' ) {
    3150         if ( empty( $post_types ) || !$this->is_post_type_archive )
     3150        if ( empty( $post_types ) || ! $this->is_post_type_archive )
    31513151            return (bool) $this->is_post_type_archive;
    31523152
    3153         $post_type_object = $this->get_queried_object();
     3153        $post_type_object = get_post_type_object( $this->get( 'post_type' ) );
    31543154
    31553155        return in_array( $post_type_object->name, (array) $post_types );
  • trunk/src/wp-includes/template-loader.php

    r22353 r25291  
    2727    if     ( is_404()            && $template = get_404_template()            ) :
    2828    elseif ( is_search()         && $template = get_search_template()         ) :
     29    elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
    2930    elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    3031    elseif ( is_front_page()     && $template = get_front_page_template()     ) :
  • trunk/src/wp-includes/template.php

    r23665 r25291  
    7171
    7272    return get_query_template( 'archive', $templates );
     73}
     74
     75/**
     76 * Retrieve path of post type archive template in current or parent template.
     77 *
     78 * @since 3.7.0
     79 *
     80 * @return string
     81 */
     82function get_post_type_archive_template() {
     83    $obj = get_post_type_object( get_query_var( 'post_type' ) );
     84    if ( ! $obj->has_archive )
     85        return '';
     86
     87    return get_archive_template();
    7388}
    7489
  • trunk/tests/phpunit/tests/query/conditionals.php

    r25287 r25291  
    649649        $this->assertQueryTrue( 'is_404' );
    650650    }
     651
     652    function test_post_type_archive_with_tax_query() {
     653        delete_option( 'rewrite_rules' );
     654
     655        $cpt_name = 'ptawtq';
     656        register_post_type( $cpt_name, array(
     657            'taxonomies' => array( 'post_tag', 'category' ),
     658            'rewrite' => true,
     659            'has_archive' => true,
     660            'public' => true
     661        ) );
     662
     663        $tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
     664        $post_id = $this->factory->post->create( array( 'post_type' => $cpt_name ) );
     665        wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
     666
     667        $this->go_to( '/ptawtq/' );
     668        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     669        $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     670
     671        add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
     672
     673        $this->go_to( '/ptawtq/' );
     674        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     675        $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     676
     677        remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
     678    }
     679
     680    function pre_get_posts_with_tax_query( &$query ) {
     681        $term = get_term_by( 'slug', 'tag-slug', 'post_tag' );
     682        $query->set( 'tax_query', array(
     683            array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
     684        ) );
     685    }
    651686}
Note: See TracChangeset for help on using the changeset viewer.