WooCommerce Get Top Seller Category Product

Today we are going to talk about how to get a top seller category product in WooCommerceor wordpress. This tutorial will guide you to place your top seller product by category in a single place. It will also help you to display your top seller category product on the listing page.

Below is an example:

$queryargs = array(
               'post_type' => 'product',
                'meta_key' => 'total_sales',
                'orderby' => 'meta_value_num',
            'order' => 'DESC',
                'post_status' => 'publish',
                'posts_per_page' => 10,
          'tax_query'             => array(
    array(
      'taxonomy'      => 'product_cat',
      'field' => 'term_id', //This is optional, as it defaults to 'term_id'
      'terms'         => $category->term_id,
      'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
      )
      ),
          );
$query = new WP_Query( $queryargs );
while ( $query->have_posts() ) {           
        $query->the_post();            
        $totalSales =   get_metadata( 'post', $query->post->ID, 'total_sales', true );
        if($totalSales){         
          if($totalSales[0] > 0) {       
              echo the_title();
          }        
      }      
}

Here is an available free WooCommerce top seller category product extension.

Qaisar Satti

Hi, I'm Qaisar Satti! I've been a developer for over 20 years, and now I love sharing what I've learned through tutorials and guides. Whether you're working with Magento, PrestaShop, or WooCommerce, my goal is to make your development journey a bit easier and more fun. When I'm not coding or writing, you can find me exploring new tech trends and hanging out with the amazing developer community. Thanks for stopping by, and happy coding!