php - 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 +word). When I insta

I have a curious problem with the Wordpress search engine of my website (the one that makes URLs as +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.

I have a curious problem with the Wordpress search engine of my website (the one that makes URLs as http://sitename?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.

Share Improve this question asked Oct 1, 2019 at 16:02 NintendomiNintendomi 53 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745110885a4611843.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信