Opened 9 years ago
Closed 9 years ago
#37151 closed defect (bug) (fixed)
WP_Term_Query not honoring orderby meta_value_num
Reported by: | littler.chicken | Owned by: | 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)
Change History (6)
#1
@
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
#3
@
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!
Hi @littler.chicken - Thanks for the report.
I was a bit bogged down too :) This is a pretty subtle issue. Briefly, the way that
WP_Term_Query
sanitizes theorderby
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 inWP_Meta_Query
: runningget_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?