WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 4 months ago

Last modified 3 months ago

#39354 closed defect (bug) (fixed)

Unable to filter term query args using `pre_get_terms` action

Reported by: jfarthing84 Owned by: boonebgorges
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.7
Component: Taxonomy Keywords: has-patch
Focuses: Cc:

Description

Thus making the action practically useless. But fear not, there is a simple remedy. In fact, the same remedy is used in WP_Query::get_posts! Use a reference within WP_Term_Query::get_terms to access the query vars locally:

$args = &$this->query_vars;

Attachments (1)

39354.patch (487 bytes) - added by jfarthing84 4 months ago.

Download all attachments as: .zip

Change History (6)

@jfarthing84
4 months ago

#1 @jfarthing84
4 months ago

  • Keywords has-patch added

#2 @boonebgorges
4 months ago

  • Component changed from General to Taxonomy
  • Milestone changed from Awaiting Review to 4.8
  • Owner set to boonebgorges
  • Status changed from new to reviewing

Good call. Thanks for the patch, @jfarthing84.

#3 @boonebgorges
4 months ago

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

In 39625:

Taxonomy: Ensure that mods to query vars in pre_term_query callbacks have an effect.

Previously, it was possible to modify the query_vars array, but
the changes would be ignored after pre_term_query had finished
running.

Props jfarthing84.
Fixes #39354.

#4 @boonebgorges
4 months ago

#39457 was marked as a duplicate.

#5 @littler.chicken
3 months ago

A note, changeset 39625 breaks code which works in 4.7 and earlier:

	$taxonomies = get_taxonomies();
	$args       = array( 'orderby' => 'count', 'order' => 'DESC' );
	$terms      = wp_get_object_terms( get_the_ID(), $taxonomies, $args );

It may be that this code is faulty and deserves to break (properly using get_object_taxonomies() works), but I thought I would mention it, in case I'm not the only one affected.

Note: See TracTickets for help on using tickets.