Zackio
3/21/2015 - 9:20 PM

Custom query and loop for WooCommerce

Custom query and loop for WooCommerce

<?php

if ( is_shop() || is_product_category() || is_product_tag() ) { // Only run on shop archive pages, not single products or other pages

	// Products per page
	$per_page = 24;

	if ( get_query_var( 'taxonomy' ) ) { // If on a product taxonomy archive (category or tag)

		$args = array(
			'post_type' => 'product',
			'posts_per_page' => $per_page,
			'paged' => get_query_var( 'paged' ),
			'tax_query' => array(
				array(
					'taxonomy' => get_query_var( 'taxonomy' ),
					'field'    => 'slug',
					'terms'    => get_query_var( 'term' ),
				),
			),
		);

	} else { // On main shop page

		$args = array(
			'post_type' => 'product',
			'orderby' => 'date',
			'order' => 'DESC',
			'posts_per_page' => $per_page,
			'paged' => get_query_var( 'paged' ),
		);

	}

	// Set the query
	$products = new WP_Query( $args );

	// Standard loop
	if ( $products->have_posts() ) :

		while ( $products->have_posts() ) : $products->the_post();

		endwhile;

		wp_reset_postdata();

	endif;

} else { // If not on archive page (cart, checkout, etc), do normal operations

	woocommerce_content();

}