1 回答

TA貢獻1818條經驗 獲得超8個贊
您可以通過傳遞參數來檢查產品是否有庫存 meta_query
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
檢查多個類別,category__and您需要傳遞一組類別 ID
'category__and' => array(1,2) // select categories with array of IDs
并檢查已發布的帖子/產品,您需要傳遞publish給post_status:
'post_status' => 'publish' // select products that are published
放在一起可能看起來像這樣(注意:未測試)
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 1,
'category__and' => array(1,2), // replace these with your cat IDs
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
}
wp_reset_postdata();
添加回答
舉報