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 a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us