WordPress   

WP loop to get related grouped products only

This code snippet can be used to get related grouped products only in single product page.

<?php
$pTerms = get_the_terms( $product->ID , 'product_cat', 'string');
$pTermsIds = wp_list_pluck($pTerms,'term_id');
$args = array(
	'post_type' => 'product',
	'post_parent' => 0,
	'post_status' => 'publish',
	'posts_per_page' => -1,
	'tax_query' => array(
	'relation' => 'AND',
	array(
		'taxonomy' => 'product_cat',
		'field' => 'id',
		'terms' => $pTermsIds,
		'include_children' => false
	),
	array(
		'taxonomy' => 'product_type',
		'field'    => 'slug',
		'terms'    => 'grouped', 
	),
),
'post__not_in' => array(get_the_ID()),
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) : ?>
<div class="related-product-container">
	<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
	<div class="related-product">
	<div class="related-product-img">
	<?php echo get_the_post_thumbnail(); ?>
	</div>
	<div class="related-product-body">
	<h3 class="related-product-title"><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></h3>
	</div>
	</div>
	<?php endwhile; ?>
</div>
<?php	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