Wordpress with woocommerce custom query

by Xenocide122   Last Updated September 26, 2017 08:08 AM

I am new to wp dev and trying to figure out how to replace or change the query woocommerce makes that shows all my products on any archive page, or page that shows my list of products, not sure what it is called but the page that shows the pic, name, price & link to product page. I have multiple categories so i guess i would need to add the code to show the different categories depending on which cat page they are on? Unless i am looking it to this too deeply.

I am trying to make an advanced loop that shows me groups of products based on a few things. First i want to see all the featured products, then i want to see the products that are not older than 6 months old, then everything else in menu order then title order ascending.

To make it even more complicated i don't want any duplicates. I think my code is working but I am not sure exactly where or how to place it so woo takes off with it and shows my results. Below is the code i am using, I am placing it in the woocommerce.php page in my child theme and it doesn't show me anything unless i echo or print_r something. So I know I am missing something but being new I don't know what I need to search for to include to see the results.

My code: (woocommerce.php)

<?php

if ( is_shop() || is_product_category() || is_product_tag() ) { // Only run on shop archive pages, not single products or other pages
    // Products per page
    $per_page = 12;
    if ( get_query_var( 'taxonomy' ) ) { // If on a product taxonomy archive (category or tag)   

        //First loop 
      $args = array(
            'post_type' => 'product',
            'orderby'  => array( 'meta_value' => 'DESC' ),
            'meta_key' => '_featured',
            'posts_per_page' => $per_page,
            'paged' => get_query_var( 'paged' ),
        );    
      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;
      rewind_posts();

      //Second loop    
      $args = array( 
          'post_type' => 'product',
          'orderby' => 'date',
          'order' => 'DESC',
          'date_query' => array(
          array(
              'before' => '6 months ago',
              ),
          ), 
            'post__not_in' => $do_not_duplicate
      );
      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;
      rewind_posts();

      //Third loop 
      $args = array(
          'post_type' => 'product',
          'orderby'  => array( 
              'menu_order' => 'ASC', 
              'title' => 'ASC',
              'post__not_in' => $do_not_duplicate
          ), 

        );  

      $loop = new WP_Query( $args );
      if (have_posts()) :        
          while ($loop->have_posts()) : $loop->the_post();    
              $post_id = get_the_ID();
              $do_not_duplicate[] = $post_id;
          endwhile;
      endif;              

    } else { // On main shop page
        $args = array(
            'post_type' => 'product',
            'orderby' => 'date',
            'order' => 'DESC',
            'posts_per_page' => $per_page,
            'paged' => get_query_var( 'paged' ),
        ); 

    }

    // Set the query
    $products = new WP_Query( $args );

    // Standard loop
    if ( $products->have_posts() ) :
        while ( $products->have_posts() ) : $products->the_post();  
        endwhile;
        wp_reset_postdata();
    endif;

} else { // If not on archive page (cart, checkout, etc), do normal operations
    woocommerce_content(); 
}

Any help to steer me in the right direction would be great help!

Update

I forgot to add 'post_type' => 'product'in my args (updated it just now), but now that I can see my titles when I add the_title(); in my while loop, how do I tell it instead to spit it all out like it would for any archive page(call a template?), also how do I make it more generic so depending on which category/archive page I am on it will just show products for that category? Thanks again!



Answers 1


    // Standard loop
if ( $products->have_posts() ) :
    while ( $products->have_posts() ) : $products->the_post();  
    endwhile;
    wp_reset_postdata();
endif;

This literally shows no data. Try adding at least the_title(); in there.

Steve North
Steve North
October 12, 2016 16:35 PM

Related Questions


Page template not displaying

Updated November 27, 2016 08:03 AM


if ( is_home() && ! is_front_page() )

Updated April 23, 2016 08:03 AM

code suddenly appearing from my tempate second loop

Updated August 10, 2016 08:03 AM