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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

How 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