Skip to content

Instantly share code, notes, and snippets.

@manutheblacker
Created November 24, 2023 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manutheblacker/06b38849a8d9d1bb0fdf57dffe684bff to your computer and use it in GitHub Desktop.
Save manutheblacker/06b38849a8d9d1bb0fdf57dffe684bff to your computer and use it in GitHub Desktop.
Query Woo products from a specific category, in stock and display in a loop
<?php
// display the title of the page related
add_filter('woocommerce_product_related_products_heading', '__return_false' );
// WC query of products of a specific categories and in stock.
$wc_query = wc_get_products( array(
'post_type' => 'product',
'post_status'=>'public',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'ids',
'terms' => $product_categories,
'operator' => 'IN',
)
),
'stock_status' => 'instock'
) );
// Display products from category by using Related products template file.
wc_get_template(
'single-product/related.php',
array(
'related_products' => $wc_query
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment