Make WordPress Core

Opened 5 years ago

Last modified 5 years ago

#50070 new defect (bug)

'post_type' query variable not set for taxonomy queries

Reported by: aurovrata's profile aurovrata Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.4
Component: Query Keywords: has-patch dev-feedback 2nd-opinion
Focuses: Cc:

Description

When a standard taxonomy query is executed on the front-end (for example loading a category term archive page to see all posts associated with that term), the $wp_query object constructed in the get_posts() function omits the post_type query variable.

The $q['post_type'] is empty initially (line 1848 wp-includes/class-wp-query.php), however, when the query is identified as a taxonomy query, a search is done to find which post_type is registered with this taxonomy (line 2132 wp-includes/class-wp-query.php) and the $post_type is set within the scope of the function, but the referenced query variable $q['post_type'] isn't updated.

this causes a problem to identify queries when hooking filters such as posts_orderby / posts_where / posts_join

Attachments (1)

50070.diff (428 bytes) - added by Howdy_McGee 5 years ago.
Set WP_Query Post Type When Empty.

Download all attachments as: .zip

Change History (7)

#1 @Howdy_McGee
5 years ago

The easiest way to see this is to print a Taxonomy archive query. Using hooks as the question mentioned posts_where and posts_join show empty post type even as the method keeps the post type in an un-passed $post_type variable.

Honestly, I think if we're going to juggle post type we should do so as the Query post type $q['post_type']. This would ensure whatever hooks appear in the future will have an accurate Post Type.

The given patch is the easy route and simply applies the current $post_type if the query post type index is empty.

@Howdy_McGee
5 years ago

Set WP_Query Post Type When Empty.

#2 @Howdy_McGee
5 years ago

  • Keywords has-patch dev-feedback 2nd-opinion added

#3 @aurovrata
5 years ago

thanks for the patch submission @Howdy_McGee. I came across another issue which this patch may not cover completely, as you are aware, for a taxonomy query

a search is done to find which post_type is registered with this taxonomy (line 2132 wp-includes/class-wp-query.php) and the $post_type is set within the scope of the function, but the referenced query variable $qpost_type? isn't updated.

however, is the taxonomy happens to be registered with more than 1 post_type then $post_type is an array, but the actual query is being made with only 1 post_type, not multiple, and therefore the above search is unable to identify which post_type is being queried.

This made me wonder if the issue faced here isn't further up the query chain, with the original contruct of the get_posts arguments.

Is there a reason why taxonomy queries do not pass the post_type in the arguments?

The typical scenario of user displaying a taxonomy query is the ability to add taxonomy term as menu items in the dashboard, and these are associated with a post_type, but the framework does not add it to the query. So maybe changing the menu item auto-generated query arguments might be a better way to solve this problem?

#4 @Howdy_McGee
5 years ago

My patch on a previous ticket might solve that issue. See #29418 and the related patch there.

This ticket was mentioned in Slack in #forums by aurovrata. View the logs.


5 years ago

This ticket was mentioned in Slack in #core by howdy_mcgee. View the logs.


5 years ago

Note: See TracTickets for help on using tickets.