Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#26749 closed defect (bug) (maybelater)

Post Type archive incorrectly identified as "home"

Reported by: michaelhall's profile michaelhall Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Query Keywords:
Focuses: template Cc:

Description

Issue

If you go to a post type archive via GET parameters ("?post_type[]=post&post_type[]=page"), then the WP_Query incorrectly identifies the location as home.

Reason fix is required

Causes incorrect templates to be used in some themes.

Steps to reproduce

  1. Setup wordpress
  2. Add the following to your theme functions file
    add_action('init', function () {
        // allow archives on the main post types
        global $wp_post_types;
    
        $wp_post_types['post']->has_archive = true;
        $wp_post_types['page']->has_archive = true;
    });
    add_action('parse_query', function(&$query) {
        // just testing for information
        if ($query->is_main_query()) {
            var_dump($query);
        }
    });
    
  3. Go to your wordpress home in your browser
  4. Add "?post_type[]=post&post_type[]=page"
    • Note that this works for any post types
    • Also note that "has_archive" on the post types does not matter

Expected Results

  • "is_home" is set to false
  • "is_archive" is set to true
  • "is_post_type_archive" is set to true

Actual Results

  • "is_home" is set to true
  • "is_archive" is set to false
  • "is_post_type_archive" is set to false

Other Notes

  • The "post" type has "has_archive" set to false by default, should consider changing it to true
  • If a single post_type is set in the URL that has "has_archive" set to false, then the template is the home page, but the query uses the selected post_type anyway
    • eg. Go to homepage with posts feed, then add "?post_type=page", the homepage will then give a feed of pages instead of posts

Attachments (1)

query.patch (1.3 KB) - added by michaelhall 11 years ago.
Patch file containing part of a possible fix for the issue

Download all attachments as: .zip

Change History (6)

@michaelhall
11 years ago

Patch file containing part of a possible fix for the issue

#1 @michaelhall
11 years ago

  • Cc mike@… added

I have provided a patch file that helps with the issue, but I believe that it does not appropriately cover all of the cases

#2 @wonderboymusic
11 years ago

I think what could happen is - when there is an array of post_types, is_archive could be true. That's it. Although, I foresee breakage.

If you made a post_type called News and wanted your homepage to show array( 'news', 'post' ) by filtering pre_get_posts, it currently is. Setting another flag would cause it to load archive.php.

#3 @wonderboymusic
11 years ago

  • Focuses template added
  • Milestone Awaiting Review deleted
  • Resolution set to maybelater
  • Status changed from new to closed

Thanks for the report, but I think this is a no-go because of guaranteed breakage. This may have been an oversight or error in the original implementation of custom post types.

Closing as maybelater - someone can reopen if they feel this can be done in back compat way.

#4 @nacin
11 years ago

This is actually by design. Post type archives are designed to be for a single post type. Otherwise, ?post_type in a URL is no different than any other query string designed to alter a blog's results (like ?order=asc). We probably could load archive.php in this case and not break a ton, but I'd rather not risk it. This is less of a maybelater than a by-design. If you want to enforce a particular template, you can do so.

Note: See TracTickets for help on using tickets.