简体   繁体   中英

Remove specific query “&cat=-22” from current url - php - wordpress relevanssi search results

I have added a button to my search results to exclude a particular category from the results (simply adds &cat=-22 to the current url).

I am trying to figure out a way to add another button to "remove" the query from the current url (ie http://host/?s=test&cat=-22 will end up being http://host/?s=test ).

If possible it needs to only remove the text specified as it is possible the query may not always be at the end of the url, ie can be

http://host/?s=test&cat=-22&orderby=post_modified&order=desc

or

http://host/?s=test&orderby=post_modified&order=desc&cat=-22

I have tried to adapt several methods listed here and elsewhere but end up removing only the 'cat' which is not great if for some reason the user clicks the link when searching a specific category, ie

http://host/?s=test&orderby=post_modified&order=desc&cat=2

becomes

http://host/?s=test&orderby=post_modified&order=desc

when link is clicked.

Hope that makes sense!

Any help with this would be very much appreciated!

Mike

I wrote this today, I think you may be able to adapt it to what you need.

What it does is accept $_SERVER['query_string'] as the $string, then just type in the $key and $value you want to remove.

public function removeGetVariable($string, $key)
{

    parse_str($string, $variables);

    unset($variables[$key]);

    return http_build_query($variables);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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