隐藏缺货- "pre_get_post;分类的可见性在商店页面


Hide out of stock - "pre_get_post" taxanomy for visibility on shop page

在我的WooCommerce商店中,我启用了"从目录中隐藏缺货商品"。该选项导致单个页面上的产品变化变得不可见

我想应用"从目录中隐藏缺货物品"选项仅在存档页面(搜索,类别)上。

下面是我的代码:
function featured_posts( $query ) {
    if ( ! $query->is_main_query() ) return;
    //What goes in here?
}
add_action( 'pre_get_posts', 'featured_posts' );

我怎样才能做到这一点?

谢谢

Try This

您必须检查页面以执行以下操作

function featured_posts( $query ) {
    if(is_product_category() || is_search()){
        if ( ! $query->is_main_query() ){
            return;
        }
    }
    ....
}
add_action( 'pre_get_posts', 'featured_posts' );