WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 3 years ago

#16320 closed defect (bug) (invalid)

pre_get_posts and category__in affects category dropdown

Reported by: mmuro Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Query Keywords: close
Focuses: 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:1 follow-up: scribu3 years ago

  • Keywords close added

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' );
Last edited 3 years ago by scribu (previous) (diff)

comment:2 ryan3 years ago

Agreed. The 3.1 behavior is correct. The 3.0.4 behavior is incorrect.

comment:3 in reply to: ↑ 1 mmuro3 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 scribu3 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.

Version 0, edited 3 years ago by scribu (next)

comment:5 scribu3 years ago

Nevermind, that would break themes in horrible ways. The current behaviour related to get_queried_object() is consistent with the one in WP 3.0.

comment:6 scribu3 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.