WP_Query: How to order by two meta_keys?
<p>Following situation: I have a custom post type called <strong>picture</strong> in Wordpress. You can rate each picture by either liking or disliking it. The result of these votes is saved as meta values in the wp_postmeta table. It consists of 3 meta_values for each picture: <strong>rating ratio</strong> in percent, <strong>sum of likes</strong> and <strong>sum of dislikes</strong></p>

<p>Now what I want to do, is creating a WP_Query which gets me the pictures first ordered by rating ratio and if the rating ratio is the same for more than one picture it should order by sum of likes. Each order criteria DESC of course.</p>

<p>I tried the following (beside many other versions that did not work):</p>

<pre><code>$args   = array('post_type'         =&gt; 'picture', 
                'posts_per_page'    =&gt; 10,
                'orderby'           =&gt; 'meta_value_num',
                'order'             =&gt; 'DESC',      
                'meta_key'          =&gt; 'rating_ratio', 
                'meta_query'        =&gt; array(
                                            array(
                                                'key' =&gt; 'rating_ratio',
                                                'value' =&gt; '0',
                                                'type' =&gt; 'NUMERIC',
                                                'compare' =&gt; '!='
                                            ),
                                            array(
                                                'key' =&gt; 'likes',
                                                'value' =&gt; '0',
                                                'type' =&gt; 'NUMERIC',
                                                'compare' =&gt; '!='
                                            )
                                        ) 
            );
</code></pre>

<p>This somehow only orders by rating_ratio. If I could have a look at the SQL-Query that Wordpress generates, I might have a chance to find the right parameters here, but <code>$GLOBALS['wp_query']-&gt;request</code> only shows me the main query.</p>

<p>Help is much appreciated.</p>

<hr>

<p>UPDATE:</p>

<p>@meet: Thx. I tried your input. Now I get the best result with the following query: </p>

<pre><code>$args   = array('post_type'         =&gt; 'picture', 
            'posts_per_page'    =&gt; 10,
            'orderby'           =&gt; 'meta_value_num meta_value_num',
            'order'             =&gt; 'DESC',      
            'meta_query'        =&gt; array(
                                        'relation' =&gt; 'AND',
                                        array(
                                            'key' =&gt; 'rating_ratio',
                                            'value' =&gt; $rating_ratio,
                                            'compare' =&gt; 'LIKE'
                                        ),
                                         array(
                                            'key' =&gt; 'likes',
                                            'value' =&gt; $likes,
                                            'compare' =&gt; 'LIKE'
                                        )
                                    ) 
        );
</code></pre>

<p>(Without the <code>orderby =&gt; meta_value_num</code> it's just sorting by date.) The problem with this new version still is, that it doesn't really order the posts by <strong>likes</strong>. It only orders them correctly by <strong>rating_ratio</strong> and then it seems to be more or less random. </p>

<p>An example: I have several posts with <code>rating_ratio=100</code> (all correctly ordered above the ones with <code>rating_ratio&lt;100</code>) where the ones with <code>likes=4</code> are falsely ordered above the one with <code>likes=5</code>.</p>

<hr>

<p><strong>FINAL WORKING SOLUTION</strong></p>

<pre><code>$args = 
array(  'post_type'         =&gt; 'picture', 
        'posts_per_page'    =&gt; 10,
        'meta_query'        =&gt; array(
                                    'relation'              =&gt; 'AND',
                                    'rating_ratio_clause'   =&gt; array(
                                                                    'key'   =&gt; 'rating_ratio',
                                                                    'type'  =&gt; 'NUMERIC',
                                                                    ),
                                     'likes_clause'         =&gt; array(
                                                                    'key'   =&gt; 'likes',
                                                                    'type'  =&gt; 'NUMERIC',
                                                                    )
                                    ),
        'orderby'           =&gt; array(
                                    'rating_ratio_clause'   =&gt; 'DESC',
                                    'likes_clause'          =&gt; 'DESC'
                                    ) 
    );
</code></pre>