2

I know I am not supposed to use query_posts

The thing is I have a program that already do that.

query_posts("");
echo "is_search():" . is_search();
query_posts("s=kucing");
echo "is_search after():" . is_search();

Now the first echo returns empty.

The second returns 1

I wonder if there is some global variable I can set to 0 so that is_search() will return false even though I did

query_posts("s=kucing");

2 Answers 2

7

Your real big problem here is the use of query_posts. It breaks the main query object, and sets the main query object to the query made by query_posts. What you are seeing is quite normal.

Your real solution here with the use of query_posts would be is to reset the main query back to what it should be. This is where wp_reset_query() comes in. If this is a normal page, is_search() will return false after wp_reset_query() as the main query is reset to the page's main query.

query_posts( '&s=crap' );
// Do your loop as normal
wp_reset_query(); // Add this after your loop
var_dump( is_search() ); // Will return bool ( false )

Remember, use of query_posts is highly discouraged. You should be using WP_Query

3
  • Great answer. I like it better. :)
    – sakibmoon
    May 31, 2015 at 14:12
  • If I reset the main query back than the themes would display the regular posts rather than those containing crap
    – user4234
    Jun 1, 2015 at 7:29
  • No, it will not. Remember, wp_reset_query() goes after you have run your loop, after you have displayed the posts from query_posts. You have to reset your main query object as plugins and other custom code relies on this object. That is why query_posts is really really bad. You cannot just set is_search() to false to expect everything to be OK. Jun 1, 2015 at 7:48
3

You can use global $wp_query and set the is_search to false;

global $wp_query;
query_posts(""); //is_search is false now
query_posts("s=kucing"); //is_search is set to true
$wp_query->is_search = false; //is_search is set to false
1
  • Let me try this one first because it seems easier
    – user4234
    Jun 1, 2015 at 7:28

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.