wppaste
WordPress

WP_Term_Query::parse_orderby( string $orderby_raw ): string|false

Since
4.6.0
Source
wp-includes/class-wp-term-query.php:918
Parse and sanitize 'orderby' keys passed to the term query.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
24–56

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 87.

Plugin surface
1 hook

Third-party callbacks on 'get_terms_orderby' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always24–50strtolower(), apply_filters()
always29–56strtolower(), apply_filters(), ->parse_orderby_meta()
!$_orderby && $_orderby !== "term_order" && $_orderby === "include" && !empty($value)42strtolower(), wp_parse_id_list(), apply_filters()
!$_orderby && $_orderby !== "term_order" && $_orderby === "slug__in" && !empty($value)47–50strtolower(), array_map(), apply_filters()
!$_orderby && $_orderby !== "term_order" && $_orderby === "include" && !empty($value)47–48strtolower(), wp_parse_id_list(), apply_filters(), ->parse_orderby_meta()
!$_orderby && $_orderby !== "term_order" && $_orderby === "slug__in" && !empty($value)52–56strtolower(), array_map(), apply_filters(), ->parse_orderby_meta()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8724–5614
8.58724–5614
8.48724–56148 fewer instructions than PHP 8.3
8.39524–6014
8.29524–6014
8.19524–6014
7.49524–6014

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:

  1. apply_filters( get_terms_orderby )filterline 954 (+36 into the body)

    Filters the ORDERBY clause of the terms query.

Uses · 3

Used by · 1

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.