wordpress / latest / classes / wp_query / parse_search_terms.html

WP_Query::parse_search_terms( string[] $terms ): string[]

Check if the terms are suitable for searching.

Description

Uses an array of stopwords (terms) that are excluded from the separate term matching when searching for posts. The list of English stopwords is the approximate search engines list, and is translatable.

Parameters

$terms string[] Required
Array of terms to check.

Return

string[] Terms that are not stopwords.

Source

File: wp-includes/class-wp-query.php. View all references

protected function parse_search_terms( $terms ) {
	$strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
	$checked    = array();

	$stopwords = $this->get_search_stopwords();

	foreach ( $terms as $term ) {
		// Keep before/after spaces when term is for exact match.
		if ( preg_match( '/^".+"$/', $term ) ) {
			$term = trim( $term, "\"'" );
		} else {
			$term = trim( $term, "\"' " );
		}

		// Avoid single A-Z and single dashes.
		if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) ) {
			continue;
		}

		if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) ) {
			continue;
		}

		$checked[] = $term;
	}

	return $checked;
}

Used By

Used By Description

Changelog

Version Description
3.7.0 Introduced.

© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_query/parse_search_terms