亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用數組過濾 Woocommerce 中的隨機產品

使用數組過濾 Woocommerce 中的隨機產品

森林海 2021-10-08 13:01:23
作為 PHP 中的菜鳥,我正在嘗試使用數組過濾掉 Woocommerce 中的產品。我設法只按類別過濾它們。我還想過濾掉那些缺貨和草稿的(如果可能的話,按數組)。還有一個問題,我如何添加'product_cat'多個類別?例如,當我想過濾連帽衫和襯衫時?對于庫存產品,我嘗試了以下代碼,但不起作用:'meta_value' => array(        array(            'key' => 'get_stock_status',            'value' => 'outofstock',            'compare' => '!='        )    )不確定,如何檢查它們是否是草稿。這是我的代碼:<ul class="products"> <?php $args = array( 'post_type' => 'product', 'orderby' => 'rand', 'posts_per_page' => 1, 'product_cat' => 'hoodies' ); $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(); ?></ul>
查看完整描述

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


查看完整回答
反對 回復 2021-10-08
  • 1 回答
  • 0 關注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號