12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on May 25, 2021

Ordering WooCommerce Products by Price

Sridhar Katakam

Looking to sort WooCommerce products in ascending order of price by default?

This can be done in two ways.

Method 1

Use the products shortcode and specify the orderby attribute’s value as price.

[products orderby="price"]

Method 2

If your theme/plugin is already using the [products] shortcode like the Products List component of Oxygen, woocommerce_shortcode_products_query filter can be used to modify the query.

Add the following code snippet:

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );
function woocommerce_shortcode_products_orderby( $args ) {
    $standard_array = array( 'menu_order', 'title', 'date', 'rand', 'id' );

    if ( isset( $args['orderby'] ) && ! in_array( $args['orderby'], $standard_array ) ) {
        $args['meta_key'] = '_price';
        $args['orderby']  = 'meta_value_num'; 
    }

    return $args;
}

Note: The above applies to all instances of the [products] shortcode.

Reference

https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-12
tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right