Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder posts by 2 custom fields and if one custom field is zero orderby another custom field
    text
    copied!<p>I have a really tricky function to implement and couldn't find a way by searching </p> <p>Please see details below </p> <p>I have a custom post type called "accommodation", and for this post type, there are 2 custom fields, one is "max_sleep_1", another one is "max_sleep_2"</p> <p>Now the thing is for some posts under this post type, "max_sleep_1" is set and for other posts "max_sleep_2" is set, But I need to order all posts by max sleep number(the maximum one between this 2 fields)</p> <p>for example</p> <p>post1 has "max_sleep_1" set to 1 and "max_sleep_2" set to 0</p> <p>post2 has "max_sleep_2" set to 2 and "max_sleep_1" set to 0</p> <p>post3 has "max_sleep_1" set to 3 and "max_sleep_2" set to 0</p> <p>so the final order of post should be post1->post2->post3, so as you can see the problem, I am ordering by 2 fields, when "max_sleep_1" is set to 0 then I need to use "max_sleep_2" to sort</p> <p>Hope you can understand what I am saying </p> <p>I have been searching but couldn't find a proper solution, I also try to merge this 2 fields after I get the results of WP_query, but also not possible so far </p> <p>now I can only order by one field using meta key and the result is post1->post3->post2, like below </p> <pre><code>$args = array( 'post_type' =&gt; 'accommodation', 'paged' =&gt; get_query_var('paged'), 'post_parent' =&gt; $parent, 'meta_key' =&gt; 'max_sleep_1', 'orderby' =&gt; 'meta_value_num', 'order' =&gt; 'ASC' ); $the_query = new WP_Query( $args ); </code></pre> <p>I will be really appreciate it if anyone can help me out </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload