Merge two queries and remove duplicate

Question

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();
0
, , hello34670 3 years 2020-05-31T19:10:19-05:00 0 Answers 86 views 0

Leave an answer

Browse
Browse