Aleš Sýkora / April 11, 2021 / 0 comments

Featured products query WooCommerce

Post summary: If you are using your own queries for WooCommerce stores, for example in Oxygen builder’s custom code block then you may need query for featured products. You can use the query I am using inside my product sliders :-). UPDATED 10.02.2022 – field = term_id was wrong, there should be name! It stopped working with…

If you are using your own queries for WooCommerce stores, for example in Oxygen builder’s custom code block then you may need query for featured products.

You can use the query I am using inside my product sliders :-).

UPDATED 10.02.2022 – field = term_id was wrong, there should be name! It stopped working with new version od Woo I am sorry! So change the field option in query to name and it will work again :o).

<div class="category-slider-big">

	<?php
		$args = array(
			'post_type' => 'product',
           	'post_status' => 'publish',
			'posts_per_page' => 12,
          'tax_query' => array( array(
            'taxonomy' => 'product_visibility',
            'field'    => 'name',
            'terms'    => 'featured',
            'operator' => 'IN',
        ) )
			);
		$loop = new WP_Query( $args );
		if ( $loop->have_posts() ) {
			while ( $loop->have_posts() ) : $loop->the_post();
          $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'medium_large'); 
          $permalink = get_permalink(get_the_ID());

?>
                      <div class="category-slider-big-cell c-padding-xs">
                <a href="<?php echo $permalink ?>">
                  <div class="c-transititon c-text-s baskerville text-up c-text-dark category-slider-text c-padding-xs"><?php the_title(); ?></div>
                  <img class="category-image" src="<?php echo $featured_img_url; ?>">
                </a>
            </div>

<?php
          
			endwhile;
		} else {
			echo __( 'No products found' );
		}
		wp_reset_postdata();
	?>
</div>

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

0 comments

Share Your Thoughts