#16347 closed defect (bug) (fixed)
is_post_type_archive doesn't return correctly outside of loop
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | 3.1 | Priority: | normal |
| Severity: | normal | Version: | 3.1 |
| Component: | Posts, Post Types | Keywords: | has-patch commit |
| Focuses: | Cc: |
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)
Change History (8)
#2
in reply to:
↑ 1
@
15 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.
#4
@
15 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.
Hmm. I haven't a clue why I wrote it that way originally. It should use get_queried_object().