Making search work with custom archive product

Hello,

So I made this custom archive-template.php to display all my products on one page, sorted by categories.
My problem is that now the search product function works only partly: if it only find one result, it redirects to the product page (good), if it doesn’t find any result it displays no products found (also good), but when it does find something it displays the whole catalog instead.
What should I add or change to my loop so that it can work? (would be nice if I could keep the results sorted by categories, but that would be a bonus :))
Thank you.

	if ( is_shop() ) {		
		$categories_args = array(
		'taxonomy' => 'product_cat'	);

		$product_categories = get_terms( $categories_args );
		if ( is_admin() ) return false;

		echo '<ul class="woocommerce">';
		// now we are looping over each term
		foreach ($product_categories as $product_category) {
			$term_id 	= $product_category->term_id;
			$term_name 	= $product_category->name;
			$term_desc 	= $product_category->description;
			$term_link 	= get_term_link($product_category->slug, $product_category->taxonomy);

			echo '<li class="cat-title" id="cat-'.$term_id.'">';
			echo '<h3>'.$term_name.'</h3>';

			$products_args = array(
				'post_type' 	=> 'product',
				'orderby' => 'title',
				'order' => 'ASC',
				'limit' => -1,
				'posts_per_page' => -1,
				'paginate'          => false,
				'page'              => 1,
				'tax_query' 	=> array( 
					array(
						'taxonomy' => 'product_cat',
						'field'    => 'term_id',
						'terms'    => $term_id, 
					),
				),
			);

			$products = new WP_Query( $products_args );

			//Looping time!
			if ( $products->have_posts() ) {
				woocommerce_product_loop_start();

					while ( $products->have_posts() ) : $products->the_post();
						wc_get_template_part( 'content', 'product' );

					endwhile; // end of the loop.

				woocommerce_product_loop_end();
				wp_reset_postdata();

				} else { 
					do_action('woocommerce_no_products_found');
					}

			echo '</li>';
		}

		echo '</ul>';//END of catalog list

	}
  • This topic was modified 1 hour, 2 minutes ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic

The page I need help with: [log in to see the link]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Your Advertisement Here