0

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.

-1

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');
  • 1
    Thank you very much. It is just what I needed. Anyway, it is not work if I don't add this line: add_filter('pre_get_posts', 'SearchFilter'); at the end of the code. Also, I changed page with array('post', 'page') to have both the types in the search results. – Nintendomi Oct 1 at 19:37

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.