B77 Codes

Studio B77 code snippets and knowledge base

Get woocommerce products by category ID

  $parent_cat = get_queried_object()->term_id;
  $args = array(
     'post_type'             => 'product',
     'post_status'           => 'publish',
     // 'ignore_sticky_posts'   => 1,
     'posts_per_page'        => -1,
     'tax_query'             => array(
         array(
             'taxonomy'      => 'product_cat',
             'field' => 'term_id', //This is optional, as it defaults to 'term_id'
             'terms'         => $parent_cat,
             'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
         ),
         // array(
         //     'taxonomy'      => 'product_visibility',
         //     'field'         => 'slug',
         //     'terms'         => 'exclude-from-catalog', // Possibly 'exclude-from-search' too
         //     'operator'      => 'NOT IN'
         // )
     ),
     'fields' => 'ids'
 );
 // $products = new WP_Query($args);
 $products = get_posts($args);

  print_r($products);