wp query - Merge two queries and remove duplicate

admin2025-01-08  9

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();

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();
Share Improve this question edited Jun 1, 2017 at 17:13 hello34670 asked Jun 1, 2017 at 15:37 hello34670hello34670 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.
}

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270808a1457.html

最新回复(0)