Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#50885 new defect (bug)

Setting a pre_get_posts query post_type to array results in broken wp-admin filters

Reported by: matpratta's profile matpratta Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4.2
Component: Query Keywords:
Focuses: administration Cc:

Description

I have a in-house plugin which creates a few extra alternate post types (eg. post_custom), and one of its requirements is that these post types are queried together with normal posts.

To do this, I've implemented a function which converts any queries for the post post type into array( 'post', 'post_custom', ... ) and it works as it should, but when on wp-admin, if one tries to go into the default "Posts" menu and filter by author or even search for anything, it will return a message saying "Invalid post type".

Checking the URL, it will contain the post_type query string as "Array" instead of "post", and changing it manually to "post" fixes the issue. I guess the wp-admin interface is not handling the array of post types correctly?

Change History (2)

#1 @SergeyBiryukov
4 years ago

  • Component changed from Administration to Query

Hi there, welcome back to WordPress Trac! Thanks for the report.

Just noting that it's generally recommended to include ! is_admin() && $query->is_main_query() checks when using pre_get_posts, so that the correct queries are altered. See Targeting the right query.

#2 @matpratta
4 years ago

Hello @SergeyBiryukov!

Thanks for pointing e in the right direction with the link! I had figured out about using the ! is_admin() part but didn't know about the latter part!

Anyways, hope the report helps somehow since that "Array" on the URL feels really out of place :)

Note: See TracTickets for help on using tickets.