Change default sorting for specific Woocommerce product category archive pages

前端 未结 3 925
孤城傲影
孤城傲影 2020-12-22 02:12

I need to change the default product sorting option to \"Newness\" for a specific product category on my site. I know you can go to WooCommerce > Settings > Product > Displa

3条回答
  •  不思量自难忘°
    2020-12-22 02:55

    Here better and WORKING solution

    add_filter('woocommerce_get_catalog_ordering_args', 'woocommerce_catalog_orderby');
    function woocommerce_catalog_orderby( $args ) {
        if( is_product_category( 'shirts' ) ) {  // <- define category slug- returns true or false
            $args['orderby']  = 'meta_value_num';
            $args['order']    = 'ASC'; // <- order ASC or DESC
            $args['meta_key'] = '_price'; // <- _price is meta_value_num's key - required
        }
        return $args;
    }
    

    Please also read about WP_Query order & orderby here: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

提交回复
热议问题