I have a shortcode that takes 1 argument for post category slug and returns and list of posts.
//[list-products] creates the product table in the wholesale page, can be used to create a table list of any category
function list_products_func ( $atts ) {
$a = shortcode_atts( array (
'category' => '',
), $atts);
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $a['category']
)
),
'post_type' => 'product',
'orderby' => 'title,'
);
$products = new WP_Query( $args );
echo "<table>";
while ( $products->have_posts() ) {
$products->the_post();
$product = new WC_Product(get_the_ID());
?>
<tr class="product-list-item">
<td><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></td>
<td>
<?php echo get_the_excerpt(); ?>
</td>
<td>
<?php echo $product->get_price_html(); ?>
</td>
<td>
<?php echo woocommerce_quantity_input(
array(
'min_value' => 1,
'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity(),
)); ?>
</td>
<td>
<?php echo woocommerce_template_loop_add_to_cart(); ?>
</td>
</tr>
<?php
}
echo "</table>";
}
add_shortcode ( 'list_products' , 'list_products_func' );
I'd like it to be able to take any number of categories and return the posts that match BOTH categories. In this use case, it is being used by a coffee distributor to show WooCommerce products that have a category of wholesale
and one of their coffee subcategories distinct-well-defined
or whatever.
The way it is used is [list-products CAT1 CAT2]
I tried just typing the shortcode with commas and hoping the code interpreted that correctly, but that didn't work.
Any help would be appreciated.
Update
It turns out I actually need it to filter products so it only shows products that have both categories, not showing all products in both categories.