How to sort WooCommerce products in category by supplied array of ids
-
Some of products in a shop I work on belong to multiple categories, so default sorting does not work.
I would like to be able to sort products in categories by supplied array of ids in the exact order so shop categories would make more sense to customers.
Hi, I have trouble completing a simple task. I tried something like this:
add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘my_ordering_function’, 20, 1 );
function my_ordering_function( $args ) {$product_category = ‘my-category’;
if( ! is_product_category($product_category) ) return $args;
$args[‘post__in’] = array( 74, 116, 3565, 3563 );
$args[‘orderby’] = ‘post__in’;
$args[‘order’] = ‘ASC’;return $args;
}Turn’s out that woocommerce_get_catalog_ordering_args does not support post__in (https://docs.woocommerce.com/wc-apidocs/source-class-WC_Query.html#499).
When I use [products ids=”114,1670,115,3565,3563″ orderby=”post__in”], it works, but I don’t want to recreate all my categories manually and use short-codes, I would prefer PHP function.
What would be you suggestion? What alternative approach would help me sort products in categories by the order post ids I supply? Thanks for any pointers.
- The topic ‘How to sort WooCommerce products in category by supplied array of ids’ is closed to new replies.