I am currently working on sorting the shop page products based on a defined category order and stock count.

I got the stock count sorting working but I am unable to find a proper way to implement sort by category along with it.

I have many categories listed in my woocommerce site in that I would like to have few category products to be listed first and rest as follows.

Currently I am using the following code to sort the products by stock count and date. I need some help on adding the category sort along with it.

add_action( 'pre_get_posts', function ( $q ) {
  if (   is_shop()                  
     && $q->is_main_query()        
     && $q->is_post_type_archive() 
    && !is_admin()
  ) {
    $q->set( 'meta_key', '_stock' );
   // $q->set( 'orderby',  'meta_value' );
    $q->set('orderby', array('meta_value' => 'DESC', 'date' => 'DESC'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.