database - ReplaceMuteStop Search Query

I'm trying to replace the search functionality in WP.I've already created the search.php template with all the

I'm trying to replace the search functionality in WP.

I've already created the search.php template with all the results I want to display.

I don't want any results from the WP database. I just want to show my results in search.php using the native searchform.php and the native ?s=keyword URL structure.

Doing that because of SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts ... search built in query with is very CPU consuming.

So in my themes functions.php I added:

add_action('pre_get_posts', 'no_search_query');
function no_search_query($query) {
    if($query->is_search() && $query->is_main_query() && get_query_var('s', false)) {
        unset( $query->query_vars['s'] );
        $query->set( 'post__in', '' );
    }
}

and added:

$is_search_query = ($_GET["s"]) ? ($_GET["s"]) : 0;

to my search.php to get the search keyword.

This actually did the trick but the query is still being called.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type IN ...

The call to the database is now a lot of less consuming (there are no arguments) but its still a call to the database which I want to eliminate.

Any idea how to do that?

Best regards.

I'm trying to replace the search functionality in WP.

I've already created the search.php template with all the results I want to display.

I don't want any results from the WP database. I just want to show my results in search.php using the native searchform.php and the native ?s=keyword URL structure.

Doing that because of SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts ... search built in query with is very CPU consuming.

So in my themes functions.php I added:

add_action('pre_get_posts', 'no_search_query');
function no_search_query($query) {
    if($query->is_search() && $query->is_main_query() && get_query_var('s', false)) {
        unset( $query->query_vars['s'] );
        $query->set( 'post__in', '' );
    }
}

and added:

$is_search_query = ($_GET["s"]) ? ($_GET["s"]) : 0;

to my search.php to get the search keyword.

This actually did the trick but the query is still being called.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.post_type IN ...

The call to the database is now a lot of less consuming (there are no arguments) but its still a call to the database which I want to eliminate.

Any idea how to do that?

Best regards.

Share Improve this question asked Jun 14, 2020 at 22:19 fat_mikefat_mike 1113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I had a quick grep for SQL_CALC_FOUND_ROWS and found a few instances. I'm not sure which one is being called in your case, but here's an interesting one from wp-includes/class-wp-query.php line 2904 onwards:

$found_rows = '';
if ( ! $q['no_found_rows'] && ! empty( $limits ) ) {
  $found_rows = 'SQL_CALC_FOUND_ROWS';
}

$old_request   = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
$this->request = $old_request;

if ( ! $q['suppress_filters'] ) {
  /**
   * Filters the completed SQL query before sending.
   *
   * @since 2.0.0
   *
   * @param string   $request The complete SQL query.
   * @param WP_Query $this    The WP_Query instance (passed by reference).
   */
  $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
}

The important part is that there's a filter here where you get the SQL and you can do your own checks on it. It doesn't look like you can stop the query running, but at least you could change the SQL to something that doesn't bother you ;-)

I don't know what the suppress_filters flag is here, so you'll need to make sure that that isn't set.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742349350a4427195.html

相关推荐

  • database - ReplaceMuteStop Search Query

    I'm trying to replace the search functionality in WP.I've already created the search.php template with all the

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信