Make WordPress Core

Opened 12 years ago

Closed 10 years ago

Last modified 10 years ago

#18703 closed defect (bug) (fixed)

'pre_get_posts'-filter without effect when modifying 'tax_query'. Wrong parameters passed to 'parse_tax_query() ??; WP 3.2.1.

Reported by: xitromedia's profile xitromedia Owned by: infoxitromediacom's profile info@…
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.2.1
Component: Query Keywords: has-patch
Focuses: Cc:


When modifying the 'tax_query' array directly with 'pre_get_posts' filter (e.g. setting 'include_children' to false), then it will firstly be modified in 'wp-includes/query.php' -> get_posts() after running the filter in line 1911.

But after line 2195 and running 'parse_tax_query' the 'tax_query' 'include_children'-parameter is unwanted reset/changed to the default value.

I think the bug is the following:

parse_tax_query() uses and needs values from the 'tax_query'-array but gets passed over only the 'query_vars'-array. so it resets the 'tax_query'-array-parameters in query.php line 1678 and hereby also the previously and individually set 'tax_query' values.

as far as i understand the core, i assume that any changes to the 'tax_query'-array with 'pre_get_posts'-filter will get lost and only changes to the 'query_vars'-array will take effect.

Attachments (1)

18703.diff (313 bytes) - added by wonderboymusic 10 years ago.

Download all attachments as: .zip

Change History (16)

#1 @dd32
12 years ago

  • Keywords reporter-feedback added

What's the exact use-case you're doing here? AFAIK, You shouldn't be modifying the tax_query for any core-handled tax queries, rather, you should be altering the query vars which create the tax query..

#2 @xitromedia
12 years ago

i want to force categories to not show posts of a child category. i think the parameter 'include_children' is made for this ?!

i found no other effective way to do this with 'pre_get_posts'.

#3 follow-up: @dd32
12 years ago

i think the parameter 'include_children' is made for this ?!

Yes, it is, but it shouldn't be modified the way you're doing it.

Instead, you should remove the 'cat' param, and set the 'category__in' param instead, which doesn't include children by default.

#4 @xitromedia
12 years ago

id did not find that rule in the documentation to not change the 'tax_query' parameter 'include_children'. what sense does it make if it must not be changed manually ?

but anyway....and only theoretically....i think the way i wanted to go by modifying the 'tax_query' should not harm in the state of 'pre_get_posts', if it is restricted to a specific category posts-query.
but this cannot work as expected because parse_tax_query() checks 'tax_query' although it never gets it (see my first post).
because of that any individual setting to 'include_children' - no matter whether done by me or by WP - will get lost/set to default i think.

#5 in reply to: ↑ 3 @dpe415
11 years ago

Replying to dd32:

Instead, you should remove the 'cat' param, and set the 'category__in' param instead, which doesn't include children by default.

This solution is fine for categories, but won't work/doesn't apply to custom taxonomies. Being able to modify include_children to exclude posts from child terms in a main custom taxonomies loop would be very, very helpful.

#6 @tollmanz
10 years ago

  • Cc tollmanz@… added

#7 @SergeyBiryukov
10 years ago

  • Component changed from General to Query
  • Keywords reporter-feedback removed

#8 @gvenk
10 years ago

  • Cc gvenk added

#9 @wonderboymusic
10 years ago

  • Keywords needs-patch added

#10 @wonderboymusic
10 years ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Awaiting Review to 3.7

Man this was obnoxious to track down - moral of the story: parse_tax_query needs an action at the end of the method just like parse_query has

That allows you to alter $query->tax_query->queries like so:

add_action( 'parse_tax_query', function ( &$query ) {
	if ( ! is_admin() && $query->is_category && ! empty( $query->tax_query->queries ) ) {
		foreach ( $query->tax_query->queries as &$tq ) {
			if ( 'category' === $tq['taxonomy'] )
				$tq['include_children'] = false;
} );

Added a patch.

#12 @wonderboymusic
10 years ago

  • Resolution set to fixed
  • Status changed from new to closed

#13 @pkwooster
10 years ago

Is there a workaround for this? This bug makes some kinds of shops in WooCommerce impossible to build. I'll apply your fix as a patch, but it means I'll have to track that in the application until it is available.

#14 follow-up: @wonderboymusic
10 years ago

add an action that alters tax_query

#15 in reply to: ↑ 14 @pkwooster
10 years ago

Replying to wonderboymusic:

add an action that alters tax_query

Thanks, any idea when this will be released?

Edit: It works perfectly!

Last edited 10 years ago by pkwooster (previous) (diff)
Note: See TracTickets for help on using tickets.