You are doing it all wrong. You should not be running custom queries in the place of the main query. Please see this post for an in-depth discussion on this issue. You should first go back to the default search page you had without the custom query. All you should have is the default loop.
It should look something like the following
<?php get_header(); ?>
<?php
$num = $GLOBALS['wp_query]->post_count;
?>
Search results for <?php the_search_query(); ?> - <?php echo $num; ?> results
if ( have_posts() ) :
$row_count=0;
while ( have_posts() ) :
$row_count++;
the_post();
global $product;
the_title(); ?>
<?php endwhile;
endif; ?>
<?php get_footer(); ?>
Now that that is sorted, we can adjust the main query by adjusting our query vars before the main query executes with pre_get_posts
.
FEW NOTES before we do
-
showposts
where dropped in favor ofposts_per_page
, so use that instead.showposts
is still valid, but it is converted intoposts_per_page
in theWP_Query
class -
You shouldn’t be setting
cache_results
to false. Because you will be making use of template tags (likethe_title()
andthe_content()
), you might end up running a huge amount of extra db queries to get data that you already have. It is almost never a good idea to setcache_results
to false. -
Before you set
update_post_term_cache
andupdate_post_meta_cache
to false, you need to be VERY VERY SURE that you will not need custom field or post term info. If you are going to use any post term info or custom field info, you are going to make a huge amount of db calls per post because these info is not stored in their relative caches.Remember,
get_the_post_thumbnail()
(which is used bythe_post_thumbnail()
) uses the custom field data stored in_thumbnail_id
to get the post thumbnail of the current post. Ifupdate_post_meta_cache
is set to false, you are going to make an extra db call per post to get that info, so 25 posts is going to mean an extra 25 db calls. The same apply to post terms, if you are going to display the terms the post belongs to, andupdate_post_term_cache
is set to false, you are going to run 25 db queries more per page loadSo be very very sure before setting those two parameters to false. In the main query loop, it is almost always a bad idea to set these to false as one normally need custom fields info like thumbnails and need to display the terms a post/products belongs to
-
If you are going to make use of pagination, be sure to remove
no_found_rows
. This parameter, when set to true, will legally break pagination -
You should be using proper naming conventions. The class is
WP_Query
, notwp_query
. In the first loop, you are usingWP_Query
and the next onewp_query
. Be consistent in your code and use the proper names of classes -
Everything you need to display numbers are already in the main query, so you can just use that
-
If you are using the default values for a parameter, you can omit it from your array of arguments. Don’t waste space and time writing them out
Lets take a look at the pre_get_posts
action to modify the main query CORRECTLY
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // Only target front end queries
&& $q->is_main_query() // Only target the main query
&& $q->is_search() // Only target search pages
) {
$q->set( 'post_type', 'product' );
$q->set( 'posts_per_page', 25 );
$q->set( 'no_found_rows', true ); // See note above
$q->set( 'update_post_term_cache', false ); // See note above
$q->set( 'update_post_meta_cache', false ); // See note above
}
});
This should solve your issue
EDIT
Crunching few numbers. The following was tested with 6 posts on my home page with post terms and post meta being used.
-
Default was 7 db queries
-
With
cache_results
set to false, I recorded 12 db queries -
With
update_post_term_cache
set to false, I recorded 33 db queries -
With
update_post_meta_cache
set to false, I recorded 11 db queries -
With all three set to false, I recorded 38 db queries
So, as you can see, make dead sure before you set these parameters to false that it is really what you need. It is always a good idea to add echo get_num_queries();
somewhere on your page to monitor the amount of db hits for the page. This will tell you whatever you are doing if it is good or bad