How to display posts by vote count and if no value continue with latest posts with no votes?

Maybe someone can help me with this. I want to display posts by votes. Users can only vote up. So what I want is – the most voted posts by vote count on top and then latest posts with no votes.
The problem with this code below is that the post is displayed only if it has at least one vote (+1). I have tested it and it sort only voted posts the way I want, but how to continue loop if no vote value?

$args = array(

'post_type' => 'topic-post',

'posts_per_page' => -1,

'order' => 'DESC',

'meta_key' => '_topic_post_votes',

'orderby' => 'meta_value_num',

'meta_query' => array(

array(

'key' => '_topic',

'value' => array( $topic_id ),

'compare' => 'IN',

)

),

'post_status' => $status

);

Related posts

Leave a Reply

1 comment

  1. The “orderby” parameter takes a fallback value, and here you want to get the posts sorted by post date when the vote value is not available. so you can just append “date” to the orderby parameter like below to get the rest of the post in the loop sorted as per the dates:–

    'orderby' => 'meta_value_num date',
    

    For more details check the Codex