I am displaying search results in order of number of views or recency. To set the initial search results view to "views" I have this in my code:

if( !isset($_REQUEST['sorting'])) {
    $_REQUEST['sorting'] = "views";

However, if I include this code, then if there are no matches, it still displays all of the results for that category. If I remove this code, the search results display correctly. How can I set the default view without destroying the search results? And why does this code affect it??



1 Answer 1


A colleague of mine gave me this helpful answer, I hope it helps others:

"To answer definitively, I would need to see a bit more code. Nonetheless, WordPress does override $_REQUEST, so depending on where this code is placed, it may be overwritten. So assuming that the parameter is being passed in the URL, I would try:

if( !isset($_GET['sorting'])) { $_GET['sorting'] = "views"; } 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.