wp_query获取多个查询中存在的帖子



我正在尝试根据选择值筛选产品,以便导出它们。4个下拉过滤器是:制造商(自定义元(、库存/缺货、数量、类别。如果用户选择了2个以上的过滤器,那么我必须获得满足所有所选过滤器的帖子(AND运算符(。我提出了一个解决方案,其中我对wp_query的结果使用array_unique(array_merge(...)),但不起作用。我怎样才能做到这一点?

这是我的代码:

$manufacturer_id = $_POST['manufacturer'];
$stock = $_POST['stock'];
$items = $_POST['items'];
$category = $_POST['category'];

//the query
if($manufacturer_id > 0){
$man_query = new WP_Query( array(
'post_type'      => array('product'),
'post_status'    => 'publish',
'posts_per_page' => -1,
'tax_query'      => array( array(
'taxonomy'        => 'pa_manufacturer',
'field'           => 'id',
'terms'           =>  array($manufacturer_id),
'operator'        => 'IN',
) )
) );
}

//stock
if($stock != '0'){ 
$stock_query = new WP_Query( array(
'post_type'      => 'product',
'post_status'    => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'key' => '_stock_status',
'value' => $stock
),
) ); 
}

//items
if($items > 0){ 
$items_query = new WP_Query( array(
'post_type'      => array('product'),
'post_status'    => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'key'     => '_stock',
'type'    => 'numeric',
'value'   => $items,
'compare' => '='
),
) ); 
}

//category
if($category > 0){
$cat_query = new WP_Query( array(
'post_type'      => array('product'),
'post_status'    => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'field'    => 'term_id',
'terms'     =>  array($category), 
'operator'  => 'IN'
),
) ); 
}
$result = new WP_Query();
$result->posts = array_unique(array_merge(
$man_query->posts,
$stock_query->posts,
$items_query->posts,
$cat_query->posts
), SORT_REGULAR);
$result->post_count = count( $result->posts );

您可以将query参数(如meta_querytax_query(组合到各自的数组中。

我试着重构上面的代码来演示:

<?php
$manufacturer_id = $_POST['manufacturer'];
$stock = $_POST['stock'];
$items = $_POST['items'];
$category = $_POST['category'];
$tax_query = array(
'relation'  => 'AND'
);
$meta_query = array(
'relation'  => 'AND'
);

//the query
if($manufacturer_id > 0){
$tax_query[] = array(
'taxonomy'        => 'pa_manufacturer',
'field'           => 'id',
'terms'           =>  array($manufacturer_id),
'operator'        => 'IN',
);
}

//stock
if($stock != '0'){ 
$meta_query[] = array(
'key' => '_stock_status',
'value' => $stock
); 
}

//items
if($items > 0){ 
$meta_query[] = array(
'key'     => '_stock',
'type'    => 'numeric',
'value'   => $items,
'compare' => '='
); 
}

//category
if($category > 0){
$meta_query[] = array(
'taxonomy'   => 'product_cat',
'field'     => 'term_id',
'terms'     =>  array($category), 
'operator'  => 'IN'
); 
}

$args = array(
'post_type'      => array('product'),
'post_status'    => 'publish',
'posts_per_page' => -1,
'tax_query'      => $tax_query,
'meta_query'     => $meta_query
);
$result = new WP_Query( $args );
if ( $result->have_posts() ) {
while( $result->have_posts() ) {
$result->the_post();
// ...
}
}

wp_reset_postdata();

点击此处了解更多信息:https://developer.wordpress.org/reference/classes/wp_query/

相关内容

  • 没有找到相关文章

最新更新