Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created March 24, 2012 00:44
Show Gist options
  • Save mikejolley/2176823 to your computer and use it in GitHub Desktop.
Save mikejolley/2176823 to your computer and use it in GitHub Desktop.
WooCommerce - Show products from current product category (when viewing a single product)
<?php
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) {
?>
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>
</ul>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_query();
}
}
?>
@NCleveland540
Copy link

Is this code meant to work with all product categories on a site or do I need to direct it at a specific child or parent?

@shadowhunt0
Copy link

how here working pagination? If i'm using wp_query?

@ssoulless
Copy link

Why is it that when I try to query posts with a parent product_cat, eg 'Our Products', I am returned posts that match a child product_cat of that parent, eg 'Our Products -> Viewfinders', even when the only product_cat selected for a product, eg 'Pocket Mini Viewfinder', is 'Viewfinders'?

So, to explain again, I have two product_cat in a hierarchical relationship, 'Our Products -> Viewfinders'. I have products in both the 'Our Products' product_cat and in the 'Viewfinders' product_cat. I want to query posts with JUST the 'Our Products' product_cat. But when I use a query like the one you posted above, I get included posts whose only product_cat is 'Viewfinders'. Is there a way around this?

hey @timothydecker I answered your question, check it out here, don't forget to give a thumbs up =)

https://stackoverflow.com/a/62666160/3700798

@mahfuj156
Copy link

Hi
working fine thank you.

mahfujit.com

@ho30hero
Copy link

Hello
If possible, please tell me how to query the current products category, featured products.
Or products of current category with tag=featured
Kind Regards

@braddalton
Copy link

Best practice to use WC_Product_Query not WP_Query for custom loops in WooCommerce.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment