php - 仅在前端将搜索结果限制为 WooCommerce 产品
问题描述
背景:
我想将搜索结果限制为仅显示 WooCommerce 产品。下面的代码正是我想要的。
//Only show products in the front-end search results
function lw_search_filter_pages($query) {
if ($query->is_search) {
$query->set('post_type', 'product');
$query->set( 'wc_query', 'product_query' );
}
return $query;
}
add_filter('pre_get_posts','lw_search_filter_pages');
问题:
通过使用上面的代码,它会影响我网站上某些插件的工作方式。例如,在 Elementor Pro 中,当我搜索 时Checkout Page
,它不会显示。相反,只会Products
出现。示例截图:
有没有办法解决这个问题?
解决方案
使用! is_admin()
会将这个过滤器限制在前端,以避免后端出现许多问题:
// Only show products in the front-end search results
add_filter('pre_get_posts','lw_search_filter_pages');
function lw_search_filter_pages($query) {
// Frontend search only
if ( ! is_admin() && $query->is_search() ) {
$query->set('post_type', 'product');
$query->set( 'wc_query', 'product_query' );
}
return $query;
}
推荐阅读
- java - 为什么这两个 java.util.Pattern 不相等
- python - 写入 CSV 文件时未过滤的行
- groovy - groovy JSON builder 需要删除空格
- android - Google Cloud Endpoint 错误:MyApi 包不存在
- java - 如何在textview中动态显示其他语言的单词
- python - 汇总存储在数据框中的多个列表
- c# - UnityEngineComponent 速度错误如何解决?
- file - Xpages 文件下载控件排序列
- mysql - 如何进行 SQL 查询以选择唯一 id 并按日期将它们排列在列中?
- php - PHP - 访问数组中的数据