Make WordPress Core

Opened 4 months ago

Last modified 4 weeks ago

#61859 new defect (bug)

meta_value_num order calse in WP_Term_Query cannot work as meta_query is evaluated after setting up the order

Reported by: gaelwanaka's profile gaelwanaka Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 6.6.1
Component: Taxonomy Keywords: reporter-feedback
Focuses: Cc:

Description

Hello,

Wordpress : 6.6.1.
Plugins : none.
Theme : any
Gutenberg : irrevelant

As the title says, when setting up orderby clause in a WP_Term_Query to "meta_num_value", the order always falls back on "name".

Thats cames from the "parse_orderby", that tries to evaluate if there is any meta query clauses, to check if the meta_value_num should be effective or not.

In the WP_Term_Query::get_terms; things are called in this order :

  • checks the meta_order conditions
    • check if there is some meta query clauses
    • if not, fallback to order by title
  • later on, parse the meta_query and initiate the clauses (on WP_Term_Query::meta_query->parse_query_vars)

So, as the meta_query is parses after checking the orderby, than the orderby is never setup to meta_value_num.

Thanks a lot,
Best regards

Change History (4)

#1 follow-ups: @pbaerne
5 weeks ago

You must define a meta_query in your WP_Term_Query even if you're not filtering by any meta fields. The meta_query tells WordPress to consider metadata for both filtering and ordering.

Last edited 5 weeks ago by pbaerne (previous) (diff)

#2 in reply to: ↑ 1 @ahrefsdr
4 weeks ago

Replying to pbaerne:

You must define a meta_query in your WP_Term_Query even if you're not filtering by any meta fields. The meta_query tells WordPress to consider metadata for both filtering and ordering.

WP_Term_Query will not work here