WooCommerce Featured Products - Featured Image Loop
<?php | |
add_shortcode ('woo_featured_products', 'woo_featured_products' ); | |
/** | |
* Create WooCommerce Image Loop of Featured Products | |
* @link https://wordpress.stackexchange.com/questions/195425/display-featured-products-through-custom-loop-in-woocommerce-on-template-page | |
*/ | |
function woo_featured_products() { | |
ob_start(); | |
$meta_query = WC()->query->get_meta_query(); | |
$tax_query = WC()->query->get_tax_query(); | |
$tax_query[] = array( | |
'taxonomy' => 'product_visibility', | |
'field' => 'name', | |
'terms' => 'featured', | |
'operator' => 'IN', | |
); | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => 1, | |
'posts_per_page' => -1, | |
'orderby' => 'date', | |
'order' => 'ASC', | |
'meta_query' => $meta_query, | |
'tax_query' => $tax_query, | |
); | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
?> | |
<div> | |
<a href="<?php echo get_permalink( $loop->post->ID ) ?>"> | |
<?php the_post_thumbnail('large'); ?> | |
</a> | |
</div> | |
<?php | |
endwhile; | |
wp_reset_query(); | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment