wordpresswoocommerce

WooCommerce Order By Price Not Working Correctly


When slecting the option sort by price (Woocommerce dropdown) the products to sort in price but it's not working exactly; the first 6 are sorted:

Link to unfinished website: http://verduijncichlids.com/product-categorie/vissen-voorraad/west-afrika-cichliden/?orderby=price

Anyone know what is happening and how to fix this? Cheers!


Solution

  • You need to use meta_value_num when you order by numeric meta data, otherwise it will compared as strings otherwise because the post meta values are stored as strings in the database.

    $args = array(
        'meta_key' => 'price',
        'orderby' => 'meta_value_num',
        'order' => 'ASC'
    );
    $query = new WP_Query( $args );