根据合并的数组获取查询结果

时间:2016-02-29 作者:Akash Pagare

第一次查询,选择找到的记录

$old_args = array(
    \'post_type\' => \'product\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\' => \'_sale_price_dates_to\',
            \'value\' => $timenow,
            \'compare\' => \'>=\',
            \'type\' => \'numeric\'
        ) ,
        array(
            \'key\' => \'_sale_price_dates_to\',
            \'value\' => \'\',
            \'compare\' => \'!=\'
        ) ,
        array(
            \'key\' => \'wccaf_location_zipcode\',
            \'value\' => array(
                \'78774\',
                \'73344\'
            ) ,
            \'compare\' => \'IN\'
        )
    ) ,
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'product_cat\',
            \'field\' => \'id\',
            \'terms\' => $cat_id
        )
    )
);
$loop1 = new WP_Query($old_args);
$posts = array();

if ($loop1->have_posts()):
    while ($loop1->have_posts()):
        $loop1->the_post();
        $posts[] = $loop1->post->ID; // Storing the post_ID\'s result in array.
    endwhile;
else:
    echo "No product Found";
endif; //loop close
第二次查询,选择未找到的记录

$new_args = array(
    \'post_type\' => \'product\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\' => \'_sale_price_dates_to\',
            \'value\' => $timenow,
            \'compare\' => \'>=\',
            \'type\' => \'numeric\'
        ) ,
        array(
            \'key\' => \'_sale_price_dates_to\',
            \'value\' => \'\',
            \'compare\' => \'!=\'
        ) ,
        array(
            \'key\' => \'wccaf_location_zipcode\',
            \'value\' => array(
                \'78774\',
                \'73344\'
            ) ,
            \'compare\' => \'NOT IN\'
        )
    ) ,
    \'orderby\' => \'meta_value\',
    \'order\' => \'ASC\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'product_cat\',
            \'field\' => \'id\',
            \'terms\' => $cat_id
        )
    )
);
$loop2 = new WP_Query($new_args);
$result_not_found_array = array();

if ($loop2->have_posts()):
    while ($loop2->have_posts()):
        $loop2->the_post();
        $posts[] = $loop2->post->ID; // Storing the post_ID\'s result in array.
    endwhile;
else:
    echo "No product Found";
endif; //loop close
将结果合并到第三个数组中

$post_ids = array_merge($result_found_array, $result_not_found_array);
$loop = new WP_Query(array(
    \'post_type\' => \'product\',
    \'post__in\' => $posts,
    \'paged\' => $paged,
    \'posts_per_page\' => 5
));
现在我想根据合并数组索引得到结果。我想显示首先找到的结果和最后未找到的结果。例如:-合并数组为101,104,103,106. 就像那样,我想要我的结果。

1 个回复
SO网友:Sumit

使用orderby post__in

$loop = new WP_Query(
    array(
        \'post_type\'      => \'product\',
        \'post__in\'       => $posts, 
        \'paged\'          => $paged,
        \'posts_per_page\' => 5,
        \'orderby\'        => \'post__in\'
    )
);
请记住,它仅在版本3.5中可用

相关推荐

Changing slug of all posts

我有一个网站,有十几种自定义帖子类型。我想更改默认的帖子类型,使其URL有一段/news/。在我的函数文件中,我有: add_action( \'init\', \'change_post_object\' ); // Change dashboard Posts to News function change_post_object() { $get_post_type = get_post_type_object(\'post\');&#x