wordpress join two queries together in main query

is there any way to query on meta data?
for example first load all posts that have most rate and after that load other posts(do not duplicate, do not change “posts_per_page”).

right now I used 2 queries for this issue, first one is query for showing most rated posts

$q = new WP_Query(
    array(
        'posts_per_page'=> -1,
        'post_type'     => 'download',
        'order'         =>  'DESC',
        'meta_key'      =>  'ratings_score',
        'orderby'       =>  'meta_value_num',
    )
);
if($q->have_posts()){while($q->have_posts()) : $q->the_post();
    $sold_out = get_post_meta($post->ID, '_edd_download_sales',true);
    if($sold_out == 1){
        continue;
    }
    $do_not_duplicate .= ",".get_the_ID();
    $str = explode(",",$do_not_duplicate);
endwhile;
}
if(is_home() && !is_paged()){
    if($q->have_posts()) : while($q->have_posts()) : $q->the_post();
        $sold_out = get_post_meta($post->ID, '_edd_download_sales',true);
        if($sold_out == 1){
            continue;
        }
        $do_not_duplicate .= ",".get_the_ID();
        $str = explode(",",$do_not_duplicate);
        $rs = get_post_meta($post->ID, 'ratings_score',true);
        $pid = get_the_ID();
        //echo '<div class="btn btn-danger">post_id='.$pid. " - <strong>rateings_score = ".$rs.'</strong></div>';
        get_template_part('content','download');
        wp_reset_postdata();
    endwhile;
    endif;
}

and the second one is showing main query

if(have_posts() ) : while (have_posts()) : the_post();
    $sold_out = get_post_meta($post->ID, '_edd_download_sales',true);
    $post_id = get_the_ID();
    if($sold_out == 1){
        continue;
    }
    if( in_array($post_id,$str) ) {
        continue;
    }else{
        get_template_part('content','download');
    }

    //the_meta();
endwhile;
else:
    get_template_part('content','none');
endif;?>
<?php if(function_exists('wp_pagenavi')){
    wp_pagenavi();
};

note: I have change main query to something like this in “functions.php”

function moortak_main_query( $query ) {
if ( $query->is_home() && $query->is_main_query() ) { // Run only on the homepage
    $query->set('post_type','download');
    $query->query_vars['posts_per_page'] = 15; // Show only 15 posts on the homepage only
}
}
// Hook my above function to the pre_get_posts action
add_action( 'pre_get_posts', 'moortak_main_query' );

Note: maybe there are better solution for this issue, hope you can help me.

Read more here: wordpress join two queries together in main query

Leave a Reply

Your email address will not be published. Required fields are marked *