WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 4 months ago

#16347 closed defect (bug) (fixed)

is_post_type_archive doesn't return correctly outside of loop

Reported by: blt4 Owned by:
Milestone: 3.1 Priority: normal
Severity: normal Version: 3.1
Component: Posts, Post Types Keywords: has-patch commit
Focuses: Cc:
PR Number:

Description

The new is_post_type_archive function depends on the loop being started ($this->posts set) to function correctly when post types are passed to the function. The function should return correctly even if the loop hasn't been started (aka in a posts_where filter).

For example, from a posts_where filter on an "event" archive page:

Correct:

is_post_type_archive() => true

Incorrect (should return true):

is_post_type_archive('event') => false

I am not a Wordpress developer, but I would replace the is_post_type_archive function in wp-includes/query.php with:

function is_post_type_archive( $post_types = '' ) {
    if ( empty( $post_types ) || !$this->is_post_type_archive )
        return (bool) $this->is_post_type_archive;
      
    return in_array( $this->query_vars['post_type'], (array) $post_types );
} 

Attachments (2)

16347.diff (605 bytes) - added by nacin 9 years ago.
Untested
16347.2.diff (596 bytes) - added by blt4 9 years ago.
How about this (replace $wp_query with $this, and post_type with name).

Download all attachments as: .zip

Change History (8)

#1 follow-up: @nacin
9 years ago

  • Milestone changed from Awaiting Review to 3.1

Hmm. I haven't a clue why I wrote it that way originally. It should use get_queried_object().

@nacin
9 years ago

Untested

@blt4
9 years ago

How about this (replace $wp_query with $this, and post_type with name).

#2 in reply to: ↑ 1 @scribu
9 years ago

  • Keywords has-patch added; is_post_type_archive post types removed

Replying to nacin:

Hmm. I haven't a clue why I wrote it that way originally. It should use get_queried_object().

I was wondering about that too.

#3 @nacin
9 years ago

  • Keywords commit added

Patch by blt4 looks proper; I was using the wrong variable.

#4 @markjaquith
9 years ago

Tested the patch with the following:

<?php

add_action( 'init', 'cws_movies_post_type' );

function cws_movies_post_type() {
	register_post_type( 'movies', array(
		'labels' => array(
			'name' => __('Movies'),
			'singular_name' => __('Movie')
			),
		'public' => true,
		'show_ui' => true,
		'rewrite' => array(
			'slug' => 'movie',
			'with_front' => false
			),
		'has_archive' => 'movies'
	) );

}

add_filter( 'posts_where', 'cws_posts_where' );

function cws_posts_where ( $where ) {
	if ( isset( $_GET['is_post_type_archive_test'] ) )
		var_dump( is_post_type_archive('movies') );
	return $where;
}

Works with the patch, but not without. In it goes.

#5 @markjaquith
9 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [17358]) Get is_post_type_archive( 'my-post-type' ) working properly outside of the loop. props bit4. fixes #16347

This ticket was mentioned in Slack in #core-editor by noisysocks. View the logs.


4 months ago

Note: See TracTickets for help on using tickets.