2017-01-25 3 views
0

商品のバリエーションを取得するためにカスタムクエリを作成しましたが、商品をゴミ箱に移動するとバリエーションステータスはpublishのままになるため、お客様の場合は404エラー破棄された製品のバリエーションを表示しようとしました。 これらのバリエーションをフィルタリングして、親製品を公開しているバリエーションのみを取得するにはどうすればよいですか?親製品が公開されている場合にのみ商品バリエーションを取得する方法

マイコード:

<?php 
$args = ['post_type' => ['product_variation'], 
      'orderby' => 'meta_value_num', 
      'order'  => 'DESC', 
      'post_status'=>'publish', 
      'product_type'=>'variation', 
      'meta_query' => [ 
       [ 
        'key'  => 'attribute_pa_flower-type', 
        'value' => $flower_type, 
        'compare' => '=', 
       ] 
      ] 
     ]; 
?> 

<?php $the_query = new WP_Query($args);?> 

<?php if ($the_query->have_posts()) : ?> 

    <div class="boxes"> 
     <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     ... 

答えて

1

はこのようないくつかのことですか:

//to hold the published product id which has vriation. 
$has_variable_ids = []; 

$args_parent = [ 
    'post_type' => ['product'], 
    'post_status' => 'publish', 
    'posts_per_page' => -1 
]; 

$pubished_post = new WP_Query($args_parent); 
if (!empty($pubished_post->posts)) 
{ 
    foreach ($pubished_post->posts as $post) 
    { 
     $_product = wc_get_product($post->ID); 
     if ($_product->is_type('variable')) 
     { 
      // Product has variations 
      $has_variable_ids[] = $post->ID; 
     } 
    } 
} 


$args = [ 
    'post_type' => ['product_variation'], 
    'orderby' => 'meta_value_num', 
    'order' => 'DESC', 
    'post_status' => 'publish', 
    'post_parent__in' => $has_variable_ids, 
    'meta_query' => [ 
     [ 
      'key' => 'attribute_pa_flower-type', 
      'value' => $flower_type, 
      'compare' => '=', 
     ] 
    ] 
]; 

$the_query = new WP_Query($args); 

を注意してください:私はそれをテストしhave't、それが動作するはずです。

は、この情報がお役に立てば幸い!

+0

ええ、私は最終的に各バリエーションの親の投稿を取得し、whileループ内のステータスを確認することですが、私のコードはあなたのものに変更されます。より速い – Mohammad

+0

が見つからず、WooCommerce関数これのために私はWPの知識を形成しました。 –

関連する問題