admin管理员组

文章数量:1336663

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?

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?

Share Improve this question edited Jun 24, 2020 at 5:50 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked Jun 23, 2020 at 19:20 TrsakTrsak 1
Add a comment  | 

1 Answer 1

Reset to default 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";
    }
}

本文标签: Get search result count in plugin