How to ensure that is_search() return false after query_posts

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

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

Leave a Comment