<?php
//retrieves the term variable from the admin page - replace "product_category" with the name of your post type
$part_terms = get_the_terms( $post->ID, 'product_category' );
if( $part_terms && !is_wp_error( $part_terms ) ) {
foreach( $part_terms as $term ) {
}
}
//create a variable to filter your Wordpress Loop
$part_args = array(
'post_type' => 'product',
'hierarchical' => true,
'posts_per_page' => -1,
'tax_query' => array(array(
'taxonomy' => 'product_category',
'field' => 'slug',
'terms' => array($term->slug),
'operator' => 'IN'
))
);
$loop = new WP_Query( $part_args );
//the loop
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
//some handy woocommerce coding
echo '<div class="background-img"><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().'<br /> '.get_the_title().'</a>';
echo $product->get_price_html();
echo '<form class="cart" method="post" enctype="multipart/form-data">
<input type="hidden" name="add-to-cart" value="';
echo esc_attr($product->id);
echo '">
<button type="submit">';
echo $product->single_add_to_cart_text();
echo '</button>
</form>';
echo '</div>';
endwhile;
?>
Run code snippet