[英]Remove out of stock products from WooCommerce related products custom WP query
您好,我想根據我的自定義查詢顯示相關產品,但我只想顯示“in_stocks”產品和 meta_query 不適用於 tax_query。 任何人都可以幫助我嗎?
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id()),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
) )
);
要從自定義 WP 查詢中刪除缺貨產品,您需要向旅游稅查詢添加一個額外的數組,如下所示:
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id() ),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array('outofstock'),
'operator' => 'NOT IN'
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
)
),
);
或者也可以通過這種方式使用元查詢:
$query_args = array(
'posts_per_page' => 10,
'no_found_rows' => 1,
'post__not_in' => array( $product->get_id() ),
'post_status' => 'publish',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
),
array(
'taxonomy' => 'product_tag',
'field' => 'id',
'terms' => $tags_array
)
),
'meta_query' => array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => '!='
)
);
它應該工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.