Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created March 24, 2012 00:44
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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();
}
}
?>
@prashantpandey10
Copy link

I am not able to access the current product id from a custom plugin I have made for some purpose. Can anyone help me in this.

@jasperf
Copy link

jasperf commented Dec 14, 2016

@timothydecker As long as you do not tick the parent category and only tick the sub category it should only load in the sub category. I realized the dummy data had albums and singles under their own categories AND in the parent category music. So that is how they got loaded all of them under different sub categories. But this is not the solution either. I still want all music items under music. I just do not want to show albums under single

@ilyosdev
Copy link

brothers who can give me sql form of this codes

@Usha-Kalamani
Copy link

@mikejolley 'hierarchical' => 1, is not working with the code given by you. Can you please guide me? Because, i want to display only one child level products.

Regards,
Usha

@asifmarp
Copy link

asifmarp commented Nov 4, 2018

@scottdurban....your code ...working fine...do you have any idea? ...how to display it as a slider?...

@erdcck
Copy link

erdcck commented Nov 13, 2018

Hi
working fine thank you. How can I do these products as html list box.
I want to add this box like as WooCommerce product variation view.

thank you.

@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