1

I have custom meta field that I use for the post rating. So I want to display posts by the date and the rating, but only for the posts that have specific rating.

For example, I want to display posts with rating above 3, and in this order: by the rating (highest first), but grouped by the date (today's best rated, then yesterday's best rated, etc.).

Can I do it with WP_Query($args=array()) class?

2
  • Hi and welcome! As you can see bellow, it is possible, but please research this site and the Codex before posting a Question. Well, research efforts are required in most (if not all) the sites of the Stack network.
    – brasofilo
    Jan 17, 2013 at 16:18
  • Thanks. I was researching but couldn't find it. Then I managed to find it on Google, but s_ha_dum already posted his answer.
    – bosniamaj
    Jan 17, 2013 at 16:40

1 Answer 1

1

Basically, I am reading right out of the Codex page for WP_Query.

You want a meta_query similar to this with an orderby parameter with the two values you want to order by. The first is dominant.

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'rating',
            'value' => 3,
            'compare' => '>'
        )
    ),
    'orderby' => 'rating post_date',
    'order' => 'DESC'
);
$query = new WP_Query( $args );

I don't know what your rating field is named and I don't have your posts and your ratings on my system so I can't test that. Hopefully that will get you started.

1
  • Yes, it works, I managed to find a tutorial with similar solution just before your answer. Thanks :)
    – bosniamaj
    Jan 17, 2013 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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