The search engine of my website finds only posts and not pages, how can I solve this problem?

I have a curious problem with the Wordpress search engine of my website (the one that makes URLs as http://sitename.com?s=searched+word). When I installed Wordpress I noticed that when I used the search engine of my website, the results was only posts... it was as if the pages did not exist. Anyway, I don't know if that is normal for Wordpress blogs. After months, I was searching a word and I noticed that in the results the pages appeared! This amazing situation has continued for a lot of time but, from last week, inexplicably, pages don't appear in the results again.

I told you the whole story to be clear, but my question is only one: How can I solve this strange behavior of my Wordpress search engine? For me it's very important that my users can also find pages.

Topic search-engines php query Wordpress

Category Web


WordPress search results can depend on your theme or plugins as well. Try enabling the default theme called Twenty Ninteen and then try your search.

The default behavior is to search both, posts and pages. However if your theme is changing that you can force the search of pages.

In order to search only pages in WordPress, we will need to add a PHP filter to the WordPress functions file. Open your functions.php file then copy and paste the code below. Your WordPress site will now return pages and posts in the search results.

function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', array('post', 'page'));
    }
    return $query;
}
add_filter('pre_get_posts', 'SearchFilter');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.