0

Is it possible to get count of search results in plugin?

I tried to use pre_get_posts action, but post_count always returned 0.

I need to know if there are any search results and if not, then do something else. I know I can edit search.php, but is it possible to make this using plugin?

1 Answer 1

0

Let's what can be done. Try the bellow code. First I'm checking if it's search query. Then trying to get the count from global query.

function search_count(){
    if(is_search()){
        global $wp_query;
        $not_singular = $wp_query->found_posts > 1 ? 'results' : 'result'; 
        echo $wp_query->found_posts . " $not_singular found";
    }
}

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.