So you will create “Street” as category using WooCommerce Default product category. Then you will create “Type” as custom taxonomy and “T-shirt” as category under that custom taxonomy. (Note that you may run into problems if you register “Product Type” as custom taxonomy because that’s WooCommerce’s default hidden taxonomy).
To guide you, I registered custom taxonomy “Type” on my local using below code. I placed this code in my theme’s functions.php file (you may not need this if you have already registered custom taxonomy):
add_action( 'init', 'pn_create_product_type_tax' );
function pn_create_product_type_tax() {
register_taxonomy(
'type',
'product',
array(
'label' => __( 'Product Type' ),
'rewrite' => array( 'slug' => 'type' ),
'hierarchical' => true,
)
);
}
Then I assigned “Street” category to some of the products and “T Shirt” category from the “Type” custom taxonomy. Then I created the shortcode using following code which I placed in my theme’s functions.php file:
add_shortcode('best_sellers', 'pn_show_best_sellers');
function pn_show_best_sellers( $atts ) {
$atts = shortcode_atts( array(
'columns' => '4',
'orderby' => 'title',
'order' => 'asc',
), $atts );
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $atts['orderby'],
'order' => $atts['order'],
'posts_per_page' => 12,
'product_cat' => 'street', /* you may need to change this as per your category slug */
'type' => 't-shirt' /* you may need to change this as per your category slug */
);
$products = new WP_Query($query_args);
$columns = $atts['columns'] ;
$woocommerce_loop['columns'] = $columns;
ob_start();
if ( $products->have_posts() ) {
?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php
} else {
echo __( 'No products found' );
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
I created simple page named “Best Seller Shirts” and placed the shortcode like below:
[best_sellers]
On front end it listed only those products which had both “Street” and “T Shirt” category assigned.
I hope this helps.
-
This reply was modified 5 years, 4 months ago by Prasad Nevase. Reason: code formatting and comments