WP_Query::parse_search_terms() WordPress Method

The parse_search_terms() method is used to parse search terms into an array. It accepts an array of terms as its parameter. The method returns an array of terms, with each term being an array of terms.

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

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.


Top ↑

Parameters

$terms

(string[])(Required)Array of terms to check.


Top ↑

Return

(string[]) Terms that are not stopwords.


Top ↑

Source

File: wp-includes/class-wp-query.php

1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
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;
}


Top ↑

Changelog

Changelog
VersionDescription
3.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More