WP_Query::parse_orderby( string $orderby ): string|false
- Since
- 4.0.0
- Source
wp-includes/class-wp-query.php:1688
Compatibility
- WordPress
- since 4.0.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- Table-prefixed value to used in the ORDER clause. False otherwise.
Performance profile
How much work a call to WP_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
- 22–108
- Plugin surface
- None
- 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 195.
Nothing here hands control to plugin code.
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()one call below WP_Query::parse_orderby()
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_Query::parse_orderby() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($meta_clauses) | 22–78 | ->get_clauses(), preg_match() |
!empty($meta_clauses) | 40–100 | ->get_clauses(), reset(), array_keys(), array_merge(), preg_match() |
empty($meta_clauses) && $orderby != "post_name" && $orderby != "post_author" && $orderby != "post_date" && $orderby != "post_title" && $orderby != "post_modified" && $orderby != "post_parent" && $orderby != "post_type" && $orderby != "ID" && $orderby != "menu_order" && $orderby != "comment_count" && $orderby != "rand" && $orderby != false && $orderby != "meta_value" && $orderby != "meta_value_num" && $orderby != "post__in" && $orderby != "post_parent__in" && $orderby != "post_name__in" | 68–77 | ->get_clauses(), preg_match(), sanitize_key() |
empty($meta_clauses) && $orderby && $orderby != "post_name" && $orderby != "post_author" && $orderby != "post_date" && $orderby != "post_title" && $orderby != "post_modified" && $orderby != "post_parent" && $orderby != "post_type" && $orderby != "ID" && $orderby != "menu_order" && $orderby != "comment_count" && $orderby != "rand" && $orderby != false && $orderby != "meta_value" && $orderby != "meta_value_num" && !empty($value) | 70–86 | ->get_clauses(), preg_match(), array_map() |
!empty($meta_clauses) && $orderby != "post_name" && $orderby != "post_author" && $orderby != "post_date" && $orderby != "post_title" && $orderby != "post_modified" && $orderby != "post_parent" && $orderby != "post_type" && $orderby != "ID" && $orderby != "menu_order" && $orderby != "comment_count" && $orderby != "rand" && $orderby != false && $orderby != "meta_value" && $orderby != "meta_value_num" && $orderby != "post__in" && $orderby != "post_parent__in" && $orderby != "post_name__in" | 86–99 | ->get_clauses(), reset(), array_keys(), array_merge(), preg_match(), sanitize_key() |
!empty($meta_clauses) && $orderby && $orderby != "post_name" && $orderby != "post_author" && $orderby != "post_date" && $orderby != "post_title" && $orderby != "post_modified" && $orderby != "post_parent" && $orderby != "post_type" && $orderby != "ID" && $orderby != "menu_order" && $orderby != "comment_count" && $orderby != "rand" && $orderby != false && $orderby != "meta_value" && $orderby != "meta_value_num" && !empty($value) | 88–108 | ->get_clauses(), reset(), array_keys(), array_merge(), preg_match(), array_map() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 195 | 22–108 | 27 | |
| 8.5 | 195 | 22–108 | 27 | |
| 8.4 | 195 | 22–108 | 27 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 208 | 25–115 | 27 | |
| 8.2 | 208 | 25–115 | 27 | 1 more instruction than PHP 8.1 |
| 8.1 | 207 | 25–115 | 27 | |
| 7.4 | 207 | 25–115 | 27 |
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.
Uses · 1
- sanitize_key()Sanitizes a string key.
Used by · 1
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
Source code
protected function parse_orderby( $orderby ) { global $wpdb; // Used to filter values. $allowed_keys = array( 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified', 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified', 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand', 'post__in', 'post_parent__in', 'post_name__in', ); $primary_meta_key = ''; $primary_meta_query = false; $meta_clauses = $this->meta_query->get_clauses(); if ( ! empty( $meta_clauses ) ) { $primary_meta_query = reset( $meta_clauses ); if ( ! empty( $primary_meta_query['key'] ) ) { $primary_meta_key = $primary_meta_query['key']; $allowed_keys[] = $primary_meta_key; } $allowed_keys[] = 'meta_value'; $allowed_keys[] = 'meta_value_num'; $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); } // If RAND() contains a seed value, sanitize and add to allowed keys. $rand_with_seed = false; if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { $orderby = sprintf( 'RAND(%s)', (int) $matches[1] ); $allowed_keys[] = $orderby; $rand_with_seed = true; } if ( ! in_array( $orderby, $allowed_keys, true ) ) { return false; } $orderby_clause = ''; switch ( $orderby ) { case 'post_name': case 'post_author': case 'post_date': case 'post_title': case 'post_modified': case 'post_parent': case 'post_type': case 'ID': case 'menu_order': case 'comment_count': $orderby_clause = "{$wpdb->posts}.{$orderby}"; break; case 'rand': $orderby_clause = 'RAND()'; break; case $primary_meta_key: case 'meta_value': if ( ! empty( $primary_meta_query['type'] ) ) { $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})"; } else { $orderby_clause = "{$primary_meta_query['alias']}.meta_value";Changelog
Introduced in 4.0.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 7.0.4 tag, from
src/wp-includes/class-wp-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.