WP_Query::parse_search( array $q ): string
- Since
- 3.7.0
- Source
wp-includes/class-wp-query.php:1423
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
$qarray- Query variables.
Return value
string- WHERE clause.
Performance profile
How much work a call to WP_Query::parse_search() 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
- Light
- Scaling
- Scales with input
- Instructions
- 68–110
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 205.
Third-party callbacks on 'post_search_columns', 'wp_query_search_exclusion_prefix' 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 - regexregular expression over the whole input
preg_match_all()called directly
Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::parse_search() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($value) && !empty($q) && empty($search) | 68–74 | stripslashes(), apply_filters(), array_intersect(), apply_filters() |
!empty($value) && empty($q) && !preg_match_all() && empty($search) | 74–80 | stripslashes(), preg_match_all(), apply_filters(), array_intersect(), apply_filters() |
!empty($value) && !empty($q) && !empty($search) | 75–86 | stripslashes(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in() |
empty($value) && !->is_main_query() && empty($q) && !preg_match_all() && empty($search) | 77–83 | stripslashes(), ->is_main_query(), preg_match_all(), apply_filters(), array_intersect(), apply_filters() |
!empty($value) && empty($q) && !preg_match_all() && !empty($search) | 81–92 | stripslashes(), preg_match_all(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in() |
empty($value) && !->is_main_query() && empty($q) && !preg_match_all() && !empty($search) | 84–95 | stripslashes(), ->is_main_query(), preg_match_all(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in() |
!empty($value) && empty($q) && preg_match_all() && empty($search) | 87–98 | stripslashes(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters() |
empty($value) && !->is_main_query() && empty($q) && preg_match_all() && empty($search) | 91–95 | stripslashes(), ->is_main_query(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters() |
!empty($value) && empty($q) && preg_match_all() && !empty($search) | 94–110 | stripslashes(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in() |
empty($value) && !->is_main_query() && empty($q) && preg_match_all() && !empty($search) | 98–107 | stripslashes(), ->is_main_query(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 205 | 68–110 | 21 | |
| 8.5 | 205 | 68–110 | 21 | |
| 8.4 | 205 | 68–110 | 21 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 217 | 71–113 | 21 | |
| 8.2 | 217 | 71–113 | 21 | |
| 8.1 | 217 | 71–113 | 21 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 218 | 71–114 | 21 |
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 · 2
2 hooks fire while WP_Query::parse_search() runs, in this order:
- apply_filters( post_search_columns )filterline 1473 (+50 into the body)
Filters the columns to search in a WP_Query search.
- apply_filters( wp_query_search_exclusion_prefix )filterline 1489 (+66 into the body)
Filters the prefix that indicates that a search term should be excluded from results.
Uses · 5
- apply_filters()Calls the callback functions that have been added to a filter hook.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- WP_Query::is_main_query()Determines whether the query is the main query.
- WP_Query::parse_search_terms()Checks if the terms are suitable for searching.
Used by · 1
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
Source code
protected function parse_search( &$q ) { global $wpdb; $search = ''; // Added slashes screw with quote grouping when done early, so done later. $q['s'] = stripslashes( $q['s'] ); if ( empty( $_GET['s'] ) && $this->is_main_query() ) { $q['s'] = urldecode( $q['s'] ); } // There are no line breaks in <input /> fields. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] ); $q['search_terms_count'] = 1; if ( ! empty( $q['sentence'] ) ) { $q['search_terms'] = array( $q['s'] ); } else { if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) { $q['search_terms_count'] = count( $matches[0] ); $q['search_terms'] = $this->parse_search_terms( $matches[0] ); // If the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) { $q['search_terms'] = array( $q['s'] ); } } else { $q['search_terms'] = array( $q['s'] ); } } $n = ! empty( $q['exact'] ) ? '' : '%'; $searchand = ''; $q['search_orderby_title'] = array(); $default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' ); $search_columns = ! empty( $q['search_columns'] ) ? $q['search_columns'] : $default_search_columns; if ( ! is_array( $search_columns ) ) { $search_columns = array( $search_columns ); } /** * Filters the columns to search in a WP_Query search. * * The supported columns are `post_title`, `post_excerpt` and `post_content`. * They are all included by default. * * @since 6.2.0 * * @param string[] $search_columns Array of column names to be searched. * @param string $search Text being searched. * @param WP_Query $query The current WP_Query instance. */ $search_columns = (array) apply_filters( 'post_search_columns', $search_columns, $q['s'], $this ); // Use only supported search columns. $search_columns = array_intersect( $search_columns, $default_search_columns ); if ( empty( $search_columns ) ) { $search_columns = $default_search_columns; } /** * Filters the prefix that indicates that a search term should be excluded from results. * * @since 4.7.0 * * @param string $exclusion_prefix The prefix. Default '-'. Returning * an empty value disables exclusions. */ $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' ); foreach ( $q['search_terms'] as $term ) { // If there is an $exclusion_prefix, terms prefixed with it should be excluded. $exclude = $exclusion_prefix && str_starts_with( $term, $exclusion_prefix ); if ( $exclude ) { $like_op = 'NOT LIKE'; $andor_op = 'AND'; $term = substr( $term, 1 ); } else { $like_op = 'LIKE'; $andor_op = 'OR'; }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 6.8.8 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.