网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress不包括搜索页面

GG网络技术分享 2025-03-18 16:12 8


问题描述:

The WP search functionality is only returning posts, not pages.

I think it\'s an issue with my WP theme. I have another WP site where it\'s fine.

One solution here suggested this in functions.php:

// include pages in search

function filter_search($query) {

if ($query->is_search) {

$query->set(\'post_type\', array(\'post\', \'page\'));

};

return $query;

};

add_filter(\'pre_get_posts\', \'filter_search\');

That works, but it causes my admin searches in the media library to turn up zero results. Any way to restrict the above to front end searches?

Or maybe there\'s something I can look for in the theme that could be causing this? It\'s a premium theme, complex, not obvious to me where to look. (my support has expired)

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

WP搜索功能仅返回帖子,而不是页面。</ p>

I 认为这是我的WP主题的一个问题。 我有另一个WP网站,没关系。</ p>

这里的一个解决方案建议在functions.php中使用:</ p>

  //包含搜索页面 

function filter_search($ query){

if($ query-&gt; is_search){

$ query-&gt; set(\'post_type\',array(\'post\',\'page\'));

}; \\ n返回$ query;

};

add_filter(\'pre_get_posts\',\'filter_search\');

</ code> </ pre>

这有效,但它会导致我在媒体上进行管理员搜索 库结果为零。 有什么方法可以限制上面到前端的搜索吗?</ p>

或许我可以在主题中寻找可能导致此问题的内容? 这是一个高级主题,复杂,对我来说并不明显。 (我的支持已过期)</ p>

</ div>

网友观点:

Try this code.

function filter_search($query) {

if ($query->is_search && !is_admin() ) {

$query->set(\'post_type\',array(\'post\',\'page\'));

}

return $query;

}

add_filter(\'pre_get_posts\',\'filter_search\');

标签:

提交需求或反馈

Demand feedback