0

I want to merge two queries together and remove duplicate. I got it working individually. Now I got it working together but the results are not mixed and the duplicates are not removed. So I got the first queries result and then the second query result...

    $fav_author_list = get_user_option( 'favorite-authors', fav_authors_get_user_id() );
    $fav_categorie_list = get_user_option( 'favorite-categories', fav_categories_get_user_id() );

    $rm_blog_args = array(
        'posts_per_page' => -1,
        'author__in'=> $fav_author_list,
        'post_type' => 'post'
    );
    $rm_blog = new WP_Query($rm_blog_args);

    $fb_args = array(
        'posts_per_page' => -1,
        'category__in'=> $fav_categorie_list,
        'post_type' => 'post'
    );
    $fb = new WP_Query($fb_args);

    // Final Query
    $final_query = new WP_Query();

    // Merging queries
    $final_query->posts = array_merge( $rm_blog->posts, $fb->posts);
    // Recount
    echo $final_query->post_count = count( $final_query->posts );


    // Remove duplicate post IDs
    $post_ids = array();
    foreach( $final_query->posts as $item ) {
        $post_ids[] = $item->ID;
    }
    $unique_posts = array_unique($post_ids);

    if($final_query->have_posts()) : while ( $final_query->have_posts() ) : $final_query->the_post();

2 Answers 2

0

Why not do something like this?

$fav_author_list = get_user_option( 'favorite-authors', fav_authors_get_user_id() );
$fav_category_list = get_user_option( 'favorite-categories', fav_categories_get_user_id() );

if(!empty($fav_author_list) && !empty($fav_category_list)) {
    $rm_args = array(
        'posts_per_page' => -1,
        'author__in'=> $fav_author_list,
        'category__in'=> $fav_category_list,
        'post_type' => 'post'
    );
} elseif(!empty($fav_author_list) && empty($fav_category_list)) {
    $rm_args = array(
        'posts_per_page' => -1,
        'author__in'=> $fav_author_list,
        'post_type' => 'post'
    );
} elseif(empty($fav_author_list) && !empty($fav_category_list)) {
    $rm_args = array(
        'posts_per_page' => -1,
        'category__in'=> $fav_category_list,
        'post_type' => 'post'
    );
} else {
    $rm_args = ''; // No args, because nothing to display.
}

if(!empty($rm_args)) {
    $rm_blog = new WP_Query($rm_args);
    if($rm_blog->have_posts()) : while ($rm_blog->have_posts()) : $rm_blog->the_post();
    // Stuff here.
}
0

Your setup makes no sense, you are querying, but not making any use of the query. Your parameters can easily be merged and used in one query unless you have to do something with the first one.

Anyway, you can store the post ids from first query in an array and then take advantage of the parameter post__not_in parameter to remove posts returned by the first query.

Your Answer

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

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