WordPress   

Custom tax query to get related woocommerce products in WordPress website

<?php	
$productTerms = get_the_terms( get_the_ID() , 'product_cat', 'string');
$productTermIDs = wp_list_pluck($productTerms,'term_id');
$args = array(
	'post_type' => 'product',
	'post_status' => 'publish',
	'posts_per_page' => -1,
	'tax_query' => array(
		array(
			'taxonomy' => 'product_cat',
			'field' => 'id',
			'terms' => $productTermIDs,
			'include_children' => false
		)
	),
	'post__not_in' => array(get_the_ID()),
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post(); 
?>
<div class="product-card">
	<?php if(has_post_thumbnail()): ?>
	<div class="product-thumb">
		<?php the_post_thumbnail( 'post-thumbnail', ['class' => 'featured-image'] ); ?>
	</div>
	<?php endif ?>
	<div class="product-content">
		<h3 class="product-title"><?php the_title(); ?></h3>
		<?php the_content(); ?>
		<a href="<?php the_permalink(); ?>">View Product</a>
	</div>
</div>
<?php endwhile; 
endif;
wp_reset_postdata();
Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us