1

I want to order a list of post on basis of ratings. I have used meta key "wp_ratings" for storing the rating value in wp_postmeta table. Now I want to sort all posts . If a post does not contains the meta key , then sort it with date.

$args = array(
    'post_type'         => 'recipe',
    'post_status'       => 'publish',
    'posts_per_page'    => 40,
    'orderby'           => array( 'meta_value_num' => 'DESC',  'date' => 'DESC' ),
    'meta_key'          => 'wp_ratings',
);

$my_query = new WP_Query( $args );

echo '<pre>';
var_dump( $my_query );
echo '</pre>';

It is still showing me only 4 post that has wp_ratings postmeta . But it is not showing me the rest 36 posts.

FYI: I am using WP 4.0.1 I am using this code, but still its not working.

4

You can pass a space-delimited set of columns to orderby argument of WP Query:

$args = array(
    'meta_key' => 'wp_ratings',
    'orderby'  => 'meta_value_num date'
);

$query = new WP_Query( $args );

You can also pass an array of key => sort_order for more granular control, for example:

$args = array(
    'meta_key' => 'wp_ratings',
    'orderby' => array (
                     'meta_value_num' => 'DESC',
                     'date'           => 'ASC'
                 )
);
$query = new WP_Query( $args );

Your actual problem seems to be related with the query and not with the order. As a meta_key has been set, the query will look for some value for that meta key. Because you want get posts with or without that meta key, you need to specify it in the meta_query:

$args = array(
    //meta key for orderby
    'meta_key'   => 'wp_ratings',
    //Meta query set to include when meta_key doesn't exist
    'meta_query' => array(
                        'relation' => 'OR',
                        array(
                            'key'     => 'wp_ratings',
                            'compare' => 'EXISTS'
                        ),
                        array(
                            'key'     => 'wp_ratings',
                            'compare' => 'NOT EXISTS'
                        )
                    ),
    'orderby'    => array (
                     'meta_value_num' => 'DESC',
                     'date'           => 'ASC'
                    )
);
$query = new WP_Query( $args );
  • I have modified the question and added the code. But still its not working. – Aftab Feb 16 '15 at 13:01
  • The code you have provided in the question is similar to the posted in the answer. It should work but I can not reproduce your problem as you are using conditionals that I can not test: for exmpale, you don't provide information about what $sort is or where that var come from, so we can not debug your code nor reproduce the issue. Test the multiple orderby without any conditional and it works (it should) your problem is anywhere else but not in the orderby agument. – cybmeta Feb 16 '15 at 13:05
  • I have updated my question with a general query which I tested with and it is still not working for me. – Aftab Feb 16 '15 at 13:31
  • 1
    See updated answer, you need to include posts when meta_key doesn't exist. – cybmeta Feb 16 '15 at 14:33
  • 1
    Beat by minutes with that answer. – Manny Fleurmond Feb 16 '15 at 14:38

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.