Last active
September 16, 2021 14:18
-
-
Save neilgee/21cafcbb2037617158a56adb7b727a78 to your computer and use it in GitHub Desktop.
WooCommerce Featured Products - Featured Image Loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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