skip to Main Content

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


  1. In your WP_Query

    replace

       $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'] ),
       ));
    

    by

    $paged = absint($_GET['product-page'] ?? 1);
    $query = new WP_Query( array(
       'post_type' => 'product_variation',
       'post_status' => 'publish',
       'posts_per_page' => 50,
       'paged' => $paged,
       'tax_query' => array(
          array(
             'taxonomy' => 'pa_maat', // The taxonomy for the attribute
             'field'    => 'slug',    // We're searching by term slug
             'terms'    => 'l',       // The term slug we're looking for
          ),
       ),
    ));
    
    Login or Signup to reply.
  2. The args are wrong.

    add_shortcode( 'single_variations', 'bs_single_variations_shortcode' );
    
    function bs_single_variations_shortcode() {  
        $args = array(
            'post_type' => 'product', // WooCommerce product post type
            'posts_per_page' => -1,   // Retrieve all products
            'post_status' => 'publish', // Only published products
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'product_type', // WooCommerce product type taxonomy
                    'field' => 'slug',
                    'terms' => 'variable', // Filter by variable products
                ),
                array(
                    'taxonomy' => 'pa_maat', // The taxonomy for the attribute
                    'field'    => 'slug',    // We're searching by term slug
                    'terms'    => 'l',       // The term slug we're looking for
                 ),
            ),
        );
        $query = new WP_Query($args);
        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;
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search