最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Select multiple categories with is_tax

programmeradmin1浏览0评论

i have a good function for exclude out of stock products from category. The problem is i want exclude for more than one category (really i want show only in one category, but i think this is the way. If someones know how show only in one category...). Here is the code. thanks!

/* Hyde out of stock product specific category */

add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' );
function hide_out_of_stock_from_cat( $query ) {
    if ( $query->is_tax( 'product_cat', 15 ) && $query->is_main_query() )     {
    $query->set( 'meta_query', array(array(
            'key'       => '_stock_status',
         'value'     => 'outofstock',
         'compare'   => 'NOT IN'
    )));
    }
}

i have a good function for exclude out of stock products from category. The problem is i want exclude for more than one category (really i want show only in one category, but i think this is the way. If someones know how show only in one category...). Here is the code. thanks!

/* Hyde out of stock product specific category */

add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' );
function hide_out_of_stock_from_cat( $query ) {
    if ( $query->is_tax( 'product_cat', 15 ) && $query->is_main_query() )     {
    $query->set( 'meta_query', array(array(
            'key'       => '_stock_status',
         'value'     => 'outofstock',
         'compare'   => 'NOT IN'
    )));
    }
}
Share Improve this question asked Sep 7, 2019 at 11:19 Vektor UnbreakableVektor Unbreakable 4311 bronze badges 5
  • 1 Just provide an array of category IDs: e.g. $query->is_tax( 'product_cat', [ 15, 16, 17 ] ) - 15, 16 and 17 are the IDs.. See is_tax(). – Sally CJ Commented Sep 7, 2019 at 11:42
  • @SallyCJ thanks, but if i use that code give me an error... I change [] with () but don't work – Vektor Unbreakable Commented Sep 7, 2019 at 12:16
  • 1 What error? Did you try array( 15, 16, 17 ) ? – Sally CJ Commented Sep 7, 2019 at 12:24
  • 1 @SallyCJ works perfect. If you put answer i check like Valid /* Hyde out of stock product specific category */ add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' ); function hide_out_of_stock_from_cat( $query ) { if ( $query->is_tax( 'product_cat', array( 15, 16, 18, 19, 20, 21, 22, 23, 24, 59, 60, 62, 63, 66 ) ) && $query->is_main_query() ) { $query->set( 'meta_query', array(array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => 'NOT IN' ))); } } – Vektor Unbreakable Commented Sep 7, 2019 at 12:42
  • 1 I'm glad it helped you, Vektor. :) – Sally CJ Commented Sep 7, 2019 at 13:09
Add a comment  | 

1 Answer 1

Reset to default 0

Thanks to @SallyCJ for the correct answer — you can supply an array of category IDs/slugs/names to is_tax() to check for multiple categories. Here is the correct code:

 /* Hyde out of stock product specific category */

 add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' );
 function hide_out_of_stock_from_cat( $query ) {
     if ( $query->is_tax( 'product_cat', array( 15, 16, 18, 19, 20, 21, 22, 23, 24, 59, 60, 62, 63, 66 ) ) && $query->is_main_query() ) {
    $query->set( 'meta_query', array(array(
            'key'       => '_stock_status',
             'value'     => 'outofstock',
             'compare'   => 'NOT IN'
    )));
     }
 }
发布评论

评论列表(0)

  1. 暂无评论