admin管理员组文章数量:1122846
A Wordpress URL that triggers a search includes a s
parameter and a value. So, something like /?s=hello.
I want Wordpress to not render the search template, but to use whatever template should be shown if the s
parameter would not be present.
I can use the template_include
hook to force another template to be used, but this still loads the wrong posts.
I can also unset the querystring variable s
using the parse_query
hook. This then loads search results as if the search was for an empty string. If I also set is_search to false, I get a fatal error as a consequence of some include files (of my theme) being loaded multiple times.
What does work is capturing the s
in template_redirect
, and redirecting to the same URL but with the s
parameter replaced by another parameter (say mys
), and then handling that parameter instead on the resulting page. I find this a hack.
How do I make this work without replacing the query string variable?
A Wordpress URL that triggers a search includes a s
parameter and a value. So, something like https://example.com/?s=hello.
I want Wordpress to not render the search template, but to use whatever template should be shown if the s
parameter would not be present.
I can use the template_include
hook to force another template to be used, but this still loads the wrong posts.
I can also unset the querystring variable s
using the parse_query
hook. This then loads search results as if the search was for an empty string. If I also set is_search to false, I get a fatal error as a consequence of some include files (of my theme) being loaded multiple times.
What does work is capturing the s
in template_redirect
, and redirecting to the same URL but with the s
parameter replaced by another parameter (say mys
), and then handling that parameter instead on the resulting page. I find this a hack.
How do I make this work without replacing the query string variable?
Share Improve this question asked May 3, 2024 at 0:00 MastaBabaMastaBaba 3113 silver badges12 bronze badges1 Answer
Reset to default 2How to get Wordpress to ignore the search parameter in the frontend?
What you can try, is hooking in earlier, to remove the public search query variable, like (untested):
add_filter( 'request', function( $qv ) {
if ( ! is_admin() && isset ( $qv['s'] ) ) {
unset( $qv['s'] );
}
return $qv;
} );
We are here hooking into WP::parse_request()
where query variables are first registered into WordPress from GET/POST request parameters.
I can also unset the query string variable s using the parse_query hook. This then loads search results as if the search was for an empty string.
Note that query variables are restored soon after the pre_get_posts
hook runs, in case they were unset:
// Fill again in case 'pre_get_posts' unset some vars.
$q = $this->fill_query_vars( $q );
https://github.com/WordPress/wordpress-develop/blob/6.5/src/wp-includes/class-wp-query.php#L1887
本文标签: How to get Wordpress to ignore the search parameter in the frontend
版权声明:本文标题:How to get Wordpress to ignore the search parameter in the frontend? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308434a1933658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论