'Problem with query products in WooCommerce with category argument in WP_Query
I tried to query products by WP_query and it was all fine. But when i tried query it with category argument nothing happenned. When i trying add new shortcode with category args it doesn't work too. I can't see where the problem is. I don't think that swiper.js could make a error. When i enter empty 'category_name' it shows all products. Others argument work as they should
function test_short($attr) {
$content = '';
$content .= "<script src='https://unpkg.com/swiper/swiper-bundle.min.js'></script>";
$sharg = shortcode_atts( array(
'cat' => '',
), $attr );
$args = array(
'post_type' => 'product',
'category_name' => 'gry',
'suppress_filters' => true
);
$wc_query = new WP_Query($args);
$content .= '<div class="swiper slidee">
<div class="swiper-wrapper">';
if($wc_query->have_posts()) {
while($wc_query->have_posts()) {
$content .= '<div class="swiper-slide">';
$wc_query->the_post();
$id = get_the_ID();
$url = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'full')[0];
$content .= '<img class="product_img" src="'.$url.'">';
$title = get_the_title();
$content .= "<div class='title'>".$title."</div>";
$product = wc_get_product( $id );
$content .= number_format(($product->get_price()),2)." zł";
$content .= '
<a href="?add-to-cart='.$id.'" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="'.$id.'" data-product_sku="" aria-label="Dodaj „'.$title.'” do koszyka" rel="nofollow"><div class="koszyk_ikona"></div><div class="koszyk_tekst">Dodaj do koszyka</div></a>
';
$content .= '</div>';
}
}
$content .= '
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div';
$content .= "<script>";
$content .= file_get_contents(get_site_url().'/javascript.js');
$content .= "</script>";
$content .= '<script type="text/javascript"> var swiper = new Swiper(".slidee", {
slidesPerView: 3.5,
spaceBetween: 30,
autoplay: {
delay: 3000,
},
speed: 1700,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
}
});
</script>';
wp_reset_query();
return $content;
}
add_shortcode("test_short","test_short");
EDIT!!!! I discovered that no of my products have category when i check them by get_the_category(). How is possible when i added them to category and at products admin panel they all have some of category
Solution 1:[1]
$category = array('Clothing', 'Bed');
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => $category
)
)
);
$loop = new WP_Query($args);
if ($loop->have_posts()):
while ($loop->have_posts()) : $loop->the_post();
global $product;
echo '<pre>';
print_r($product->get_name());
echo '</pre>';
endwhile;
endif;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mujuonly |
