覆盖wordpress中的搜索功能(sql和php) [英] overwrite the search function in wordpress (sql and php)
问题描述
有没有办法覆盖wordpress中的默认搜索功能?我试过使用过滤器,但它们只允许添加到查询中……或者可能使用posts_request 重写整个查询.但是,如果我覆盖它,则其他查询都不起作用.我有以下代码
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');
我可以用我的自定义 SQL 覆盖 $input,但是页面上有一个小部件显示最近的帖子,如果我这样做就不会显示.有没有办法只覆盖搜索功能??
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 there a way to JUST overwrite the search function??
推荐答案
这不是万无一失的,但假设第一个 WP_Query
调用是针对搜索请求的(有可能 是插件在 WordPress 之前调用它的场景,但不太可能),一旦函数运行,您就可以去除过滤器.
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;
}
这篇关于覆盖wordpress中的搜索功能(sql和php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!