Nick
Nick

Reputation: 14293

Woocommerce products query - order_by

I am using a query in my template.php file to display wooCommerce products by category.

My query looks like this:

<?php
   $args = array(
     'post_type' => 'product',
     'stock' => 1,
     'posts_per_page' => 99,
     'tax_query' => array(
          'relation' => 'OR',
           array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'system_components',
              'menu_order' => 'asc'
           ),
           array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'add-ons',
              'menu_order' => 'asc'
           )
      ),
      'meta_query' => array(
            array(
               'key'       => '_visibility',
               'value'     => 'hidden',
               'compare'   => '!='
      )
    ));


   $loop = new WP_Query($args);
   if ($loop->have_posts()) {
     while ($loop->have_posts()) :
          $loop->the_post(); ?>
            .....

This works, and I can see the products I want and the one I don't want are not visible.

The only bit I don't understand is that 'menu_order' => 'ASC' doesn't seem to work.

I doesn't' matter what I type as menu order in the product settings, the order doesn't change.

What am I doing wrong here?

Thanks

Upvotes: 6

Views: 4904

Answers (2)

Avag Sargsyan
Avag Sargsyan

Reputation: 2523

Remove 'menu_order' => 'asc' part from tax_query and add to main, that should work:

$args = array(
    'post_type' => 'product',
    'stock' => 1,
    'posts_per_page' => 99,
    'orderby' => 'menu_order',
    'order' => 'ACS',
    'tax_query' => array(
        'relation' => 'OR',
        array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'system_components'
        ),
        array(
              'taxonomy' => 'product_cat',
              'field' => 'slug',
              'terms' => 'add-ons'              
        )
    ),
    'meta_query' => array(
        array(
            'key'       => '_visibility',
            'value'     => 'hidden',
            'compare'   => '!='
        )
    ));

As you can see in the documentation, the tax_query hasn't parameter menu_order.

Upvotes: 5

user5490424
user5490424

Reputation:

Please, Set page order in your product.

For this go to Admin panel. Click on Products menu from left menu, Then edit product and set page order.

Upvotes: 0

Related Questions