1

If search results are empty then I want that some code be executed, but how to write such situations programmatically?

If !is_search() {

certain code

}

But above doesn't work.

2

Your code will check whether you are on a search page or not ( You also forgot to wrap your conditional in parentheses ).

In order to check if there is any result for your search, use have_posts():

if( have_posts() ) {
    // There is a post
} else {
    // No results
}

This works for global queries. If you wrote your own instance of WP_Query(), you need to do as follows:

$my_query = new WP_Query($args);

if( $my_query->have_posts() ){
    // There is a post
} else {
    // No results
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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