简体   繁体   English

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

[英]overwrite the search function in wordpress (sql and php)

Is there a way to overwrite the default search function in wordpress? 有没有办法覆盖wordpress中的默认搜索功能? I have tried using the filters, but they only allow adding to the query... or possibly rewriting the whole query using posts_request. 我尝试使用过滤器,但它们仅允许添加到查询中……或可能使用posts_request重写整个查询。 If I overwrite that though, no other querys will work. 如果我将其覆盖,则没有其他查询可用。 I have the following code 我有以下代码

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

add_filter('posts_request','my_posts_request_filter');

I could override $input with my custom SQL, but there is a widget on the page which shows recent posts and that wouldn't show if I do this. 我可以使用自定义SQL覆盖$ input,但是页面上有一个小部件,它显示最近的帖子,如果执行此操作,则不会显示。 Is there a way to JUST overwrite the search function?? 有没有办法覆盖搜索功能?

This isn't bulletproof, but assuming the first WP_Query call is for the search request (there might be a scenario where a plugin calls it before WordPress does, but it is unlikely), you can strip the filter once the function runs. 这不是防弹的,但假设第一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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM