为了账号安全,请及时绑定邮箱和手机立即绑定

仅在前端将搜索结果限制为 WooCommerce 产品

仅在前端将搜索结果限制为 WooCommerce 产品

PHP
波斯汪 2023-10-22 21:50:03
背景:我想将搜索结果限制为仅显示 WooCommerce 产品。下面的代码正是我想要的。//Only show products in the front-end search resultsfunction 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出现。截图示例:有办法解决这个问题吗?

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

使用! 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;

}


查看完整回答
反对 回复 2023-10-22

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号