posts_search

最后更新于:2021-11-27 15:37:20

apply_filters_ref_array( ‘posts_search’, string $search, GC_Query $query )

Filters the search SQL that is used in the WHERE clause of GC_Query.

参数

$search

(string)
Search SQL for WHERE clause.

$query

(GC_Query)
The current GC_Query object.

源文件

文件: gc-includes/class-gc-query.php

View on Trac

function __search_by_title_only( $search, &$gc_query )
{
    global $gcdb;
    if(empty($search)) {
        return $search; // skip processing - no search term in query
    }
    $q = $gc_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array)$q['search_terms'] as $term) {
        $term = esc_sql($gcdb->esc_like($term));
        $search .= "{$searchand}($gcdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($gcdb->posts.post_password = '') ";
    }
    return $search;
}
add_filter('posts_search', '__search_by_title_only', 500, 2);