Opened 2 years ago
Closed 23 months ago
#16320 closed defect (bug) (invalid)
pre_get_posts and category__in affects category dropdown
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | |
| Component: | Query | Version: | 3.1 |
| Severity: | normal | Keywords: | close |
| Cc: |
Description
If you add the following code as a plugin or to your functions file, the category dropdown on the Posts edit screen will select the category instead of defaulting to 'View all categories'.
add_filter( 'pre_get_posts','my_posts_query' );
function my_posts_query( $query ){
$query->set( 'category__in', array(1) );
return $query;
}
I tried changing the query variable to category NOT in to test and it correctly defaults to 'View all categories'.
The above code works properly in 3.0.4.
Change History (6)
comment:3
in reply to:
↑ 1
mmuro
— 2 years ago
Replying to scribu:
Of course it selects the category, because it's the same as going to /wp-admin/edit.php?cat=1
What I'm saying is that the correct behaviour is the current one, not the one in 3.0.4.
If you don't want that code to affect the admin area, just add an is_admin() check:
if ( !is_admin() ) add_filter( 'pre_get_posts','my_posts_query' );
And if you have multiple categories in the array?
comment:4
scribu
— 2 years ago
That's something else.
Maybe we should make get_queried_object() return NULL when multiple terms are involved, since it's not reliable in that case anyway.
It wouldn't have any backwards-compatibility implications (theoretically).
Of course it selects the category, because it's the same as going to /wp-admin/edit.php?cat=1
What I'm saying is that the correct behaviour is the current one, not the one in 3.0.4.
If you don't want that code to affect the admin area, just add an is_admin() check: