开发者

overwrite the search function in wordpress (sql and php)

Is there a way to overwrite the default search function in wordpress? I have tried using the filters, but they only allow adding to the query... or possibly rewriting the whole query using 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. Is ther开发者_StackOverflow中文版e 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.

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜