0

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 (i.e. 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, i.e. 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, i.e.

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

1

1 Answer 1

0

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);

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response @Geilt, though I am unsure of how to utalize this code, I get an error if I leave it as a public function? Could you advise how the link should be created?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.