I have 15 product categories and 50+ products into my WooCommerce website. I want to arrange (sort) products from a specific category with an array of product ids.
This is my code attempt:
add_filter( 'woocommerce_get_catalog_ordering_args', 'my_ordering_function', 10, 2 );
function my_ordering_function( $args ) {
$product_category = 'engineering-service';
if( ! is_product_category($product_category) ) return $args;
$args['post__in'] = array( 181, 8622, 8689, 8832 );
$args['orderby'] = 'post__in';
$args['order'] = 'ASC';
return $args;
}
This doesn't work.
Which hooked function do I need to use? How can I do that programmatically?
You are not using the right hook and the right way. Try the following:
add_filter( 'woocommerce_product_query', 'engineering_service_product_query', 10, 2 );
function engineering_service_product_query( $q, $query ) {
$product_category = array('engineering-service');
$product_ids = array( 181, 8622, 8689, 8832 );
if( is_product_category( $product_category ) ) {
$q->set('post__in', $product_ids );
$q->set('orderby', 'post__in');
$q->set('order', 'ASC');
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.