I’m trying to generate a list of products that belong to the attribute pa_maat
and term L
.
The snippet below creates a list of products and all it’s variations.
How do I alter the code to only show products for the term L
?
Updated:
Attribute: pa_maat
Terms: S
,M
,L
,XL
,2XL
,3XL
Attribute: pa_kleur
Terms: wit
,sportgrijs
,zwart
Basicly the script should display 3 variations per product as each of the 3 colors come in size L
.
Shortcode
[single_variations]
Code snippet
add_shortcode( 'single_variations', 'bs_single_variations_shortcode' );
function bs_single_variations_shortcode() {
$query = new WP_Query( array(
'post_type' => 'product_variation',
'post_status' => 'publish',
'posts_per_page' => 50,
'paged' => absint( empty( $_GET['product-page'] ) ? 1 : $_GET['product-page'] ),
));
if ( $query->have_posts() ) {
ob_start();
wc_setup_loop(
array(
'name' => 'single_variations',
'is_shortcode' => true,
'is_search' => false,
'is_paginated' => true,
'total' => $query->found_posts,
'total_pages' => $query->max_num_pages,
'per_page' => $query->get( 'posts_per_page' ),
'current_page' => max( 1, $query->get( 'paged', 1 ) ),
)
);
woocommerce_pagination();
woocommerce_product_loop_start();
while ( $query->have_posts() ) {
$query->the_post();
wc_get_template_part( 'content', 'product' );
}
woocommerce_product_loop_end();
woocommerce_pagination();
wp_reset_postdata();
wc_reset_loop();
return ob_get_clean();
}
return;
}
2
Answers
In your WP_Query
replace
by
The args are wrong.