Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#37151 closed defect (bug) (fixed)

WP_Term_Query not honoring orderby meta_value_num

Reported by: littlerchicken's profile littler.chicken Owned by: boonebgorges's profile boonebgorges
Milestone: 4.6 Priority: normal
Severity: normal Version: 4.6
Component: Taxonomy Keywords: has-patch needs-testing
Focuses: Cc:

Description

I think this is related to ticket #35381, the introduction of the WP_Term_Query class. I have a situation where I've created a taxonomy and pre-populated it with about 60 terms. The order is not alphabetical or anything, but should be consistent.

If I could be sure that no one would add new terms, I would order by ID and leave it at that, but new terms might be added in certain cases, so I've created a term metadata key and used a number for it to set the order. I've put a filter on get_terms_defaults to set the orderby to use the new term metadata.

In WP 4.5, this works--looks like 4.4 did not accept meta_value and meta_value_num for orderby yet. However, in trunk, the meta_value_num no longer works. As near as I can tell, the reason is that

$meta_clauses = $this->meta_query->get_clauses();

(line 817 of class-wp-term-query.php) is returning an empty value, so the $orderby parameter is returned as empty as well. I get completely bogged down when I try to diagnose any further in the WP_Meta_Query class, so I'm currently stumped. However, since it works in 4.5 and not in 4.6, I'm thinking it's a bug, so reporting it in the hopes that someone else can help sort this.

Attachments (1)

37151.diff (2.0 KB) - added by boonebgorges 9 years ago.

Download all attachments as: .zip

Change History (6)

#1 @ocean90
9 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 4.6
  • Owner set to boonebgorges
  • Status changed from new to reviewing

#2 @boonebgorges
9 years ago

  • Keywords has-patch needs-testing added; needs-unit-tests removed

Hi @littler.chicken - Thanks for the report.

I get completely bogged down when I try to diagnose any further in the WP_Meta_Query class

I was a bit bogged down too :) This is a pretty subtle issue. Briefly, the way that WP_Term_Query sanitizes the orderby parameter means that the meta query's SQL clauses must have been generated. This logic was changed in 4.6, which is why your previously-working code doesn't work anymore. It's possible to force the meta query to generate its SQL, but this uncovers a bug in WP_Meta_Query: running get_sql() doesn't reset the internal list of table aliases, which results in malformed SQL in some cases.

Could you test 37151.diff to see if it addresses your issue?

@boonebgorges
9 years ago

#3 @littler.chicken
9 years ago

Boone, thanks so much (and bonus points for making it look easy!). Tested your patch and it does correct the issue for me, in my scenario (sorting by term metadata/meta_value_num).

Thank you!

#4 @boonebgorges
9 years ago

  • Status changed from reviewing to accepted

@littler.chicken Thanks so much for verifying!

#5 @boonebgorges
9 years ago

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

In 37860:

Fix orderby meta handling for WP_Term_Query.

In order to allow meta-related values of orderby to be handled properly,
the term query's meta_query object must run its get_sql() method before
orderby parsing.

Fixing this bug required addressing another bug in WP_Meta_Query, which
caused the table alias index not to be reset when calling get_sql()
multiple times on the same object.

Props littler.chicken.
Fixes #37151.

Note: See TracTickets for help on using tickets.