Home > Software design >  Why are the products not shown?
Why are the products not shown?

Time:02-21

<?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 >
              <li >
                 <div >
                    <div >
                        <span ><?php the_title();?></span><br>
                      <span><?php the_content();?></span>
                    </div>
                    <div ><?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(); 
      
} ?>

CodePudding user response:

$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

  • Related