WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 10 months ago

Last modified 10 months 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 Owned by: info@…
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.2.1
Component: Query Keywords: has-patch
Focuses: Cc:

Description

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 11 months ago.

Download all attachments as: .zip

Change History (16)

comment:1 dd323 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..

comment:2 xitromedia3 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'.

comment:3 follow-up: dd323 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.

comment:4 xitromedia3 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.

comment:5 in reply to: ↑ 3 dpe4152 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.

comment:6 tollmanz13 months ago

  • Cc tollmanz@… added

comment:7 SergeyBiryukov13 months ago

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

comment:8 gvenk12 months ago

  • Cc gvenk added

comment:9 wonderboymusic11 months ago

  • Keywords needs-patch added

wonderboymusic11 months ago

comment:10 wonderboymusic11 months 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.

comment:12 wonderboymusic10 months ago

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

comment:13 pkwooster10 months 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.

comment:14 follow-up: wonderboymusic10 months ago

add an action that alters tax_query http://core.trac.wordpress.org/changeset/25311

comment:15 in reply to: ↑ 14 pkwooster10 months ago

Replying to wonderboymusic:

add an action that alters tax_query http://core.trac.wordpress.org/changeset/25311

Thanks, any idea when this will be released?

Version 0, edited 10 months ago by pkwooster (next)
Note: See TracTickets for help on using tickets.