S
S
Sergei Gurdjiyan2017-03-18 00:32:37
WordPress
Sergei Gurdjiyan, 2017-03-18 00:32:37

How to get Woocommerce products by attribute?

How to correctly compose parameters for new WP_Query($params)in order to get products from a certain category and with the desired attribute. I am using the following parameters, which do not work:

array(4) {
  ["post_type"]=>string(7) "product"
  ["cat"]=>int(7)
  ["posts_per_page"]=>int(100)
  ["meta_query"]=>
  array(2) {
    ["relation"]=>string(3) "AND"
    [0]=>
      array(3) {
        ["meta_key"]=>string(15) "pa_video-genres"
        ["value"]=>string(3) "pop"
        ["compare"]=>string(1) "="
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robert AW, 2017-03-18
@Robert_bl

<?php $args = array( 
'tax_query' => array( // массив массивов состояящий из:
 array( 
'taxonomy' => 'product_cat', // название Таксономии
'field' => 'slug', // Производить выбор по 'id' или 'slug' 
'terms' => 'microzaim' //  ID(ы) или ярлык(и) таксономии, в зависимости от предыдущего параметра
)), 
'posts_per_page' => '7', // Кол-во выводимых товаров на одной странице
'post_type' => 'product', // Тип записи
'order' => 'ASC' // порядок сортировки ASC - по возрастанию, DESC - по убыванию(по умолчанию)
); ?>

<?php  add_image_size( 'custom-size', 100, 50, true ); ?> <!-- Задаёт размер изображение -->

<table border="3" id="micro-zaim"> 
<tr>
  <th>Банк</th>
  <th>Рейтинг</th>
  <th>Срок</th>
  <th>Переплата</th>
</tr>

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

<?php while ( $loop->have_posts() ) : ?>
  <?php $loop->the_post(); ?>
  <?php global $product; ?>
  <tr>
    <td><?php echo $product->get_image('custom-size'); the_title(); ?></td>
    <td><?php echo $product->get_attribute('pa_rating'); ?></td>
    <td><?php echo $product->get_attribute('pa_time'); ?></td>
    <td><?php echo $product->get_attribute('pa_overpayments'); ?></td>
  </tr>
<?php endwhile; ?>

</table>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question