'WooCommerce sidebar filter widgets not working with custom product WP_Query
I am creating a custom template to display all products that have a positive stock value. I have also manually included the shop sidebar into my template as well using dynamic_sidebar. However the product attribute filter widgets in my sidebar aren't displaying, I assume because those widgets don't know how to 'talk' to my WP_Query to know what products are being returned and what attribute filters to even show. If I manually add the filter query string to the URL (for example - ?filter_color=gray) it's not filtering my WP_Query either, I assume because this is a WooCommerce query parameter, not a Wordpress / WP_Query thing.
So I guess my questions are, 1. is there a way to connect my WP_Query to WooCommerce's attribute filter sidebar widgets, and or is there a better way to build out a custom template that's showing only products with positive stock values that's also filterable using WooCommerce's attribute filter widgets?
My WP_Query:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 1000,
'meta_query' => array(
array(
'key' => '_stock',
'value' => '1',
'compare' => '>',
)
),
'meta_key' => '_stock',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
My sidebar code:
<?php dynamic_sidebar( 'shop-sidebar' ); ?>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
