WP_Query::parse_search_order( array $query_vars ): string
- Since
- 3.7.0
- Source
wp-includes/class-wp-query.php:1629
Compatibility
- WordPress
- since 3.7.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
$query_varsarray- Query variables.
Return value
string- ORDER BY clause.
Performance profile
How much work a call to WP_Query::parse_search_order() 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
- 11–68
- 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 74.
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 one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::parse_search_order() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11 | reset() |
| always | 18–33 | none |
| always | 26–41 | ->esc_like() |
| always | 27–42 | ->prepare() |
| always | 35–50 | ->esc_like(), ->prepare() |
| always | 36–51 | ->prepare(), ->prepare() |
| always | 44–59 | ->esc_like(), ->prepare(), ->prepare() |
| always | 45–60 | ->prepare(), ->prepare(), ->prepare() |
| always | 53–68 | ->esc_like(), ->prepare(), ->prepare(), ->prepare() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 74 | 11–68 | 7 | |
| 8.5 | 74 | 11–68 | 7 | |
| 8.4 | 74 | 11–68 | 7 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 83 | 11–77 | 7 | |
| 8.2 | 83 | 11–77 | 7 | |
| 8.1 | 83 | 11–77 | 7 | |
| 7.4 | 83 | 11–77 | 7 |
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
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
Source code
protected function parse_search_order( &$query_vars ) { global $wpdb; if ( $query_vars['search_terms_count'] > 1 ) { $num_terms = count( $query_vars['search_orderby_title'] ); // If the search terms contain negative queries, don't bother ordering by sentence matches. $like = ''; if ( ! preg_match( '/(?:\s|^)\-/', $query_vars['s'] ) ) { $like = '%' . $wpdb->esc_like( $query_vars['s'] ) . '%'; } $search_orderby = ''; // Sentence match in 'post_title'. if ( $like ) { $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like ); } /* * Sanity limit, sort as sentence when more than 6 terms * (few searches are longer than 6 terms and most titles are not). */ if ( $num_terms < 7 ) { // All words in title. $search_orderby .= 'WHEN ' . implode( ' AND ', $query_vars['search_orderby_title'] ) . ' THEN 2 '; // Any word in title, not needed when $num_terms == 1. if ( $num_terms > 1 ) { $search_orderby .= 'WHEN ' . implode( ' OR ', $query_vars['search_orderby_title'] ) . ' THEN 3 '; } } // Sentence match in 'post_content' and 'post_excerpt'. if ( $like ) { $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like ); $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like ); } if ( $search_orderby ) { $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)'; } } else { // Single word or sentence search. $search_orderby = reset( $query_vars['search_orderby_title'] ) . ' DESC'; } return $search_orderby; }Changelog
Introduced in 3.7.0. 2 changes between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$query_vars added.verified against source$q removed.verified against sourceAbout 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.