1

This are my current parameters for WP_Query:

array(5) {
  ["posts_per_page"]=>
  int(6)
  ["orderby"]=>
  array(2) {
    ["uss_product_price"]=>
    string(3) "ASC"
    ["uss_product_weight"]=>
    string(4) "DESC"
  }
  ["meta_query"]=>
  array(3) {
    ["relation"]=>
    string(3) "AND"
    ["sortprimary_clause"]=>
    array(2) {
      ["key"]=>
      string(17) "uss_product_price"
      ["compare"]=>
      string(6) "EXISTS"
    }
    ["sortsecondary_clause"]=>
    array(2) {
      ["key"]=>
      string(18) "uss_product_weight"
      ["compare"]=>
      string(6) "EXISTS"
    }
  }
  ["paged"]=>
  string(1) "1"
  ["post_type"]=>
  string(7) "product"
}

This new kind of orderby was introduced in WordPress 4.0 and 4.2: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

The query basically works however the products are ordered by the string value of uss_product_price and uss_product_weight. Previously this problem was solved by giving meta_value_num instead of meta_value in WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

I want the result to be ordered by the numeric value of uss_product_price (first) and uss_product_weight (second). How can I tell the query to use the numeric value while still using the complexity of multiple orderby parameters?

2

Boohoo why did I start a bounty for that ;-) The answer is pretty simple - add 'type' => 'numeric' to the meta query.

array(5) {
  ["posts_per_page"]=>
  int(6)
  ["orderby"]=>
  array(2) {
    ["uss_product_price"]=>
    string(4) "DESC"
    ["uss_product_weight"]=>
    string(4) "DESC"
  }
  ["meta_query"]=>
  array(3) {
    ["relation"]=>
    string(3) "AND"
    ["sortprimary_clause"]=>
    array(3) {
      ["key"]=>
      string(17) "uss_product_price"
      ["compare"]=>
      string(6) "EXISTS"
      ["type"]=>
      string(7) "numeric"
    }
    ["sortsecondary_clause"]=>
    array(2) {
      ["key"]=>
      string(18) "uss_product_weight"
      ["compare"]=>
      string(6) "EXISTS"
    }
  }
  ["paged"]=>
  string(1) "1"
  ["post_type"]=>
  string(7) "product"
}
  • 1
    Glad you solved your own bounty, be sure to mark your own answer as accepted. – Ethan Jinks O'Sullivan Aug 29 '16 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.