使用wooccommerce从循环中排除产品


Exclude Products from loop using woocommerce

我必须使用wooccommerce排除类别循环产品页面中的一些产品。只有在元表中具有特定值的产品才会被排除在外。我已经写了下面的代码,但这对我不起作用。请有人帮我。

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
      if ( ! $q->is_main_query() ) return;
      if ( ! $q->is_post_type_archive() ) return;
     $q->set( 'meta_query', array(array(
                 array(
                     'key' => '_auction_closed',
                      'compare' => 'NOT EXISTS'
                    )
                 )
               )
             );
    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
}

尝试以下代码:

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    $meta_query = $q->get('meta_query');
    $meta_query[] = array(
        'key'=>'_auction_closed',
        'compare'=>'NOT EXISTS',
        );
    $q->set('meta_query',$meta_query);
    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
}