<?php add_action('wp_ajax_nopriv_filter', 'filter_ajax'); add_action('wp_ajax_filter', 'filter_ajax'); function filter_ajax(){ $category = $_POST['category']; $args = array( 'taxonomy' => 'product_cat', 'post_type'=>'product', 'posts_per_page' => -1, ); if(isset($category)) { $args['category__in'] = array($category); }
The category id is delivered, but it is not clear to me why the products of this selected category are not displayed
$loop = new WP_Query($args); if($loop->have_posts()): while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <ul class="list-group list-group-flush list-group-products"> <li class="list-group-item select-blue-essence"> <div class="row in-list-text"> <div class="col-4 text-center font-poppins js-filter"> <span class="fw-bold"><?php the_title();?></span><br> <span><?php the_content();?></span> </div> <div class="col-4 price-tag font-poppins fw-bold"><?php echo $product->get_price_html(); ?> <?php woocommerce_template_loop_add_to_cart(); ?></div> </div> </li> </ul> </div> <?php endwhile; endif; wp_reset_postdata(); die(); } ?>
Advertisement
Answer
$category = isset($_POST['category']) :?array(absint($_POST['category'])) : array(); $args = array( 'posts_per_page' => -1, 'post_type' => 'product', 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $category ) ) ); $loop = new WP_Query($args);
Try this – Category IDs should be integer