Jana
Jana

Reputation: 430

Sort categories list by counting item

For example, I have 5 categories in Woocommerce like

A(3) ( 3 for the items in category A )

B(7)

C(22)

D(1)

E(0)

They are display in category page and ordered by something like id.

And I want rearrange the results to be display in categories list page that sort from higher to lower, like

C(22)

B(7)

A(3)

D(1)

E(0)

Is there any solution with using the hook pre_get_posts ( or other hook ) to fix this issue?

I have tried to test the hook woocommerce_before_subcategory and it just shows me one category at a time and I cannot edit any one of them.

Upvotes: 1

Views: 241

Answers (2)

Jana
Jana

Reputation: 430

After testing the code in Woocommerce, and I found a solution with woocommerce_product_subcategories_args hook.

add_filter('woocommerce_product_subcategories_args', 'ProductSubcategoryArguments', 999, 1);

function ProductSubcategoryArguments($arg=array()){
    $arg['order']='DESC';
    $arg['orderby']='count';
    return $arg;
}

Upvotes: 1

SimBeez
SimBeez

Reputation: 175

Yes there is possible, see wp_list_category

<?php wp_list_category('orderby=count'); ?>

Upvotes: 1

Related Questions