wppaste
WordPress

WP_Site_Query::parse_orderby( string $orderby ): string|false

Since
4.6.0
Source
wp-includes/class-wp-site-query.php:789
Parses and sanitizes 'orderby' keys passed to the site 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

$orderbystring
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_Site_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
7–74

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

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_Site_Query::parse_orderby() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7–41none
empty($parsed) && !empty($value) && empty($meta_clauses)15–47->get_clauses()
always21–27array_map()
empty($parsed) && !empty($value) && !empty($meta_clauses)24–74->get_clauses(), reset()
empty($parsed) && !empty($value) && empty($meta_clauses)29–33array_map(), ->get_clauses()
empty($parsed) && !empty($value) && !empty($meta_clauses)38–60array_map(), ->get_clauses(), reset()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1327–7426
8.51327–7426
8.41327–74268 fewer instructions than PHP 8.3
8.31407–7426
8.21407–74262 more instructions than PHP 8.1
8.11387–8926
7.41387–8926

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.

Used by · 1

Source code

	protected function parse_orderby( $orderby ) {		global $wpdb; 		$parsed = false; 		switch ( $orderby ) {			case 'site__in':				$site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );				$parsed   = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";				break;			case 'network__in':				$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );				$parsed      = "FIELD( {$wpdb->blogs}.site_id, $network__in )";				break;			case 'domain':			case 'last_updated':			case 'path':			case 'registered':			case 'deleted':			case 'spam':			case 'mature':			case 'archived':			case 'public':				$parsed = $orderby;				break;			case 'network_id':				$parsed = 'site_id';				break;			case 'domain_length':				$parsed = 'CHAR_LENGTH(domain)';				break;			case 'path_length':				$parsed = 'CHAR_LENGTH(path)';				break;			case 'id':				$parsed = "{$wpdb->blogs}.blog_id";				break;		} 		if ( ! empty( $parsed ) || empty( $this->meta_query_clauses ) ) {			return $parsed;		} 		$meta_clauses = $this->meta_query->get_clauses();		if ( empty( $meta_clauses ) ) {			return $parsed;		} 		$primary_meta_query = reset( $meta_clauses );		if ( ! empty( $primary_meta_query['key'] ) && $primary_meta_query['key'] === $orderby ) {			$orderby = 'meta_value';		} 		switch ( $orderby ) {			case 'meta_value':				if ( ! empty( $primary_meta_query['type'] ) ) {					$parsed = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";				} else {					$parsed = "{$primary_meta_query['alias']}.meta_value";				}				break;			case 'meta_value_num':				$parsed = "{$primary_meta_query['alias']}.meta_value+0";				break;			default:				if ( isset( $meta_clauses[ $orderby ] ) ) {					$meta_clause = $meta_clauses[ $orderby ];					$parsed      = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";				}		} 		return $parsed;	}

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 7.0.4 tag, from src/wp-includes/class-wp-site-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.