覆盖wordpress中的搜索功能(sql和php)

Mat*_*cer 3 php wordpress

有没有办法覆盖wordpress中的默认搜索功能?我试过使用过滤器,但它们只允许添加到查询中……或者可能使用posts_request 重写整个查询。但是,如果我覆盖它,则其他查询都不起作用。我有以下代码

function my_posts_request_filter($input)
{
    if ( is_search() && isset($_GET['s'])) {
        global $wpdb;
    }
    return $input;
}

add_filter('posts_request','my_posts_request_filter');
Run Code Online (Sandbox Code Playgroud)

我可以用我的自定义 SQL 覆盖 $input,但页面上有一个小部件显示最近的帖子,如果我这样做就不会显示。有没有办法只覆盖搜索功能?

The*_*dic 6

这不是万无一失的,但假设第一次WP_Query调用是针对搜索请求的(可能存在插件在 WordPress 之前调用它的情况,但不太可能),您可以在函数运行后去除过滤器。

function my_posts_request_filter($input)
{
    if ( is_search() && isset($_GET['s'])) {
        global $wpdb;

        // do your funky SQL

        remove_filter('posts_request','my_posts_request_filter');
    }
    return $input;
}
Run Code Online (Sandbox Code Playgroud)