wppaste
WordPress

WP_Query::parse_search( array $query_vars ): string

Since
3.7.0
Source
wp-includes/class-wp-query.php:1423
Generates SQL for the WHERE clause based on passed search terms.

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
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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
68–110

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

Plugin surface
2 hooks

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.

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
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
!empty($value) && !empty($query_vars) && empty($search)68–74stripslashes(), apply_filters(), array_intersect(), apply_filters()
!empty($value) && empty($query_vars) && !preg_match_all() && empty($search)74–80stripslashes(), preg_match_all(), apply_filters(), array_intersect(), apply_filters()
!empty($value) && !empty($query_vars) && !empty($search)75–86stripslashes(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in()
empty($value) && !->is_main_query() && empty($query_vars) && !preg_match_all() && empty($search)77–83stripslashes(), ->is_main_query(), preg_match_all(), apply_filters(), array_intersect(), apply_filters()
!empty($value) && empty($query_vars) && !preg_match_all() && !empty($search)81–92stripslashes(), preg_match_all(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in()
empty($value) && !->is_main_query() && empty($query_vars) && !preg_match_all() && !empty($search)84–95stripslashes(), ->is_main_query(), preg_match_all(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in()
!empty($value) && empty($query_vars) && preg_match_all() && empty($search)87–98stripslashes(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters()
empty($value) && !->is_main_query() && empty($query_vars) && preg_match_all() && empty($search)91–95stripslashes(), ->is_main_query(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters()
!empty($value) && empty($query_vars) && preg_match_all() && !empty($search)94–110stripslashes(), preg_match_all(), ->parse_search_terms(), apply_filters(), array_intersect(), apply_filters(), is_user_logged_in()
empty($value) && !->is_main_query() && empty($query_vars) && preg_match_all() && !empty($search)98–107stripslashes(), ->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

PHPCompiledExecutedBranchesNotes
8.6-dev20568–11021
8.520568–11021
8.420568–1102112 fewer instructions than PHP 8.3
8.321771–11321
8.221771–11321
8.121771–113211 fewer instruction than PHP 7.4
7.421871–11421

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:

  1. apply_filters( post_search_columns )filterline 1473 (+50 into the body)

    Filters the columns to search in a WP_Query search.

  2. 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

Used by · 1

Source code

	protected function parse_search( &$query_vars ) {		global $wpdb; 		$search = ''; 		// Added slashes screw with quote grouping when done early, so done later.		$query_vars['s'] = stripslashes( $query_vars['s'] );		if ( empty( $_GET['s'] ) && $this->is_main_query() ) {			$query_vars['s'] = urldecode( $query_vars['s'] );		}		// There are no line breaks in <input /> fields.		$query_vars['s']                  = str_replace( array( "\r", "\n" ), '', $query_vars['s'] );		$query_vars['search_terms_count'] = 1;		if ( ! empty( $query_vars['sentence'] ) ) {			$query_vars['search_terms'] = array( $query_vars['s'] );		} else {			if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $query_vars['s'], $matches ) ) {				$query_vars['search_terms_count'] = count( $matches[0] );				$query_vars['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( $query_vars['search_terms'] ) || count( $query_vars['search_terms'] ) > 9 ) {					$query_vars['search_terms'] = array( $query_vars['s'] );				}			} else {				$query_vars['search_terms'] = array( $query_vars['s'] );			}		} 		$n                                  = ! empty( $query_vars['exact'] ) ? '' : '%';		$searchand                          = '';		$query_vars['search_orderby_title'] = array(); 		$default_search_columns = array( 'post_title', 'post_excerpt', 'post_content' );		$search_columns         = ! empty( $query_vars['search_columns'] ) ? $query_vars['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, $query_vars['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 ( $query_vars['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.

  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.

6.9.7
Parameter $query_vars added.verified against source
6.9.7
Parameter $q removed.verified against source
3.7.0
Introduced.from the docblock

About this page

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