Show products under $55 in order of lowest price [duplicate]
First, do not use WP_Query()
or get_posts()
. From the WooCommerce doc:
wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.
See WooCommerce documentation
Second, you cannot order by price directly in the query. Get your products then call the wc_products_array_orderby()
function.
$args = array(); // Optional arguments
$products = wc_get_products( $args );
$ordered = wc_products_array_orderby( $products, 'price', 'ASC' );