WordPress   

Custom tax query loop to get products from specific product categories in WordPress

$args = array(
	'post_type' => 'product',
	'post_status' => 'publish',
	'posts_per_page' => 20,
	'tax_query' => array(
		array(
			'taxonomy' => 'product_cat',
			'field' => 'term_id',
			'terms' => array(21,22)
		)
	),
);
$loop = new WP_Query($args);
if ( $loop->have_posts() ) :
	while ( $loop->have_posts() ) : $loop->the_post(); ?>
	<div class="product-card">
		<?php echo get_the_post_thumbnail(); ?>
		<h2 class="product-title"><?php echo get_the_title(); ?></h2>
		<a href="<?php echo get_permalink(); ?>">View Product</a>
	</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