$selected_cat = isset($_GET['cat']) ? sanitize_text_field($_GET['cat']) : ''; $args = array( 'post_type' => 'product', 'posts_per_page' => 12, ); // If a category is selected, filter products by that category if (!empty($selected_cat)) { $args['tax_query'] = array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $selected_cat, ), ); } $loop = new WP_Query($args); if ($loop->have_posts()) : echo '
'; while ($loop->have_posts()) : $loop->the_post(); global $product; // Your product card here endwhile; echo '
'; else : echo '

No products found in this category.

'; endif; wp_reset_postdata();