WP_Term_Query::parse_orderby( string $orderby_raw ): string|false
- Since
- 4.6.0
- Source
wp-includes/class-wp-term-query.php:918
Compatibility
- WordPress
- since 4.6.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$orderby_rawstring- Alias for the field to order by.
Return value
string|false- Value to used in the ORDER clause. False otherwise.
Performance profile
How much work a call to WP_Term_Query::parse_orderby() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 24–56
- Plugin surface
- 1 hook
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.
Third-party callbacks on 'get_terms_orderby' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Term_Query::parse_orderby() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 24–50 | strtolower(), apply_filters() |
| always | 29–56 | strtolower(), apply_filters(), ->parse_orderby_meta() |
!$_orderby && $_orderby !== "term_order" && $_orderby === "include" && !empty($value) | 42 | strtolower(), wp_parse_id_list(), apply_filters() |
!$_orderby && $_orderby !== "term_order" && $_orderby === "slug__in" && !empty($value) | 47–50 | strtolower(), array_map(), apply_filters() |
!$_orderby && $_orderby !== "term_order" && $_orderby === "include" && !empty($value) | 47–48 | strtolower(), wp_parse_id_list(), apply_filters(), ->parse_orderby_meta() |
!$_orderby && $_orderby !== "term_order" && $_orderby === "slug__in" && !empty($value) | 52–56 | strtolower(), array_map(), apply_filters(), ->parse_orderby_meta() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 87 | 24–56 | 14 | |
| 8.5 | 87 | 24–56 | 14 | |
| 8.4 | 87 | 24–56 | 14 | 8 fewer instructions than PHP 8.3 |
| 8.3 | 95 | 24–60 | 14 | |
| 8.2 | 95 | 24–60 | 14 | |
| 8.1 | 95 | 24–60 | 14 | |
| 7.4 | 95 | 24–60 | 14 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while WP_Term_Query::parse_orderby() runs, in this order:
- apply_filters( get_terms_orderby )filterline 954 (+36 into the body)
Filters the ORDERBY clause of the terms query.
Uses · 3
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Term_Query::parse_orderby_meta()Generate the ORDER BY clause for an 'orderby' param that is potentially related to a meta query.
Used by · 1
- WP_Term_Query::get_terms()Retrieves the query results.
Source code
protected function parse_orderby( $orderby_raw ) { $_orderby = strtolower( $orderby_raw ); $maybe_orderby_meta = false; if ( in_array( $_orderby, array( 'term_id', 'name', 'slug', 'term_group' ), true ) ) { $orderby = "t.$_orderby"; } elseif ( in_array( $_orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id', 'description' ), true ) ) { $orderby = "tt.$_orderby"; } elseif ( 'term_order' === $_orderby ) { $orderby = 'tr.term_order'; } elseif ( 'include' === $_orderby && ! empty( $this->query_vars['include'] ) ) { $include = implode( ',', wp_parse_id_list( $this->query_vars['include'] ) ); $orderby = "FIELD( t.term_id, $include )"; } elseif ( 'slug__in' === $_orderby && ! empty( $this->query_vars['slug'] ) && is_array( $this->query_vars['slug'] ) ) { $slugs = implode( "', '", array_map( 'sanitize_title_for_query', $this->query_vars['slug'] ) ); $orderby = "FIELD( t.slug, '" . $slugs . "')"; } elseif ( 'none' === $_orderby ) { $orderby = ''; } elseif ( empty( $_orderby ) || 'id' === $_orderby || 'term_id' === $_orderby ) { $orderby = 't.term_id'; } else { $orderby = 't.name'; // This may be a value of orderby related to meta. $maybe_orderby_meta = true; } /** * Filters the ORDERBY clause of the terms query. * * @since 2.8.0 * * @param string $orderby `ORDERBY` clause of the terms query. * @param array $args An array of term query arguments. * @param string[] $taxonomies An array of taxonomy names. */ $orderby = apply_filters( 'get_terms_orderby', $orderby, $this->query_vars, $this->query_vars['taxonomy'] ); // Run after the 'get_terms_orderby' filter for backward compatibility. if ( $maybe_orderby_meta ) { $maybe_orderby_meta = $this->parse_orderby_meta( $_orderby ); if ( $maybe_orderby_meta ) { $orderby = $maybe_orderby_meta; } } return $orderby; }Changelog
Introduced in 4.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wp-term-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.