0

I have a WordPress website with WooCommerce plugin installed.

There is one problem that i cannot figure out why do I do not got any search results shown up when I do product search?

Website URL with search query:

http://www.v-tac.hr/?s=round&post_type=product

Website URL (I use search on my left blue sidebar - topbar search is not yet configured):

http://www.v-tac.hr/

I have edited default search.php of WordPress theme, code here:

<?php get_header(); ?>
<?php
$search = get_query_var("s");
$mySearch =& new WP_Query("s=$search&showposts=-1");
$num = $mySearch->post_count;
?>
Search results for <?php the_search_query(); ?> - <?php echo $num; ?> results
<?php
$argss = array ( 
    'orderby' => 'date',
    's' => $search,
    'order' => 'DESC', 
    'showposts' => 25, // 12
    'no_found_rows' => true,
    'post_type'=>'product',
    'post_status' => 'publish',
    'cache_results' => false,
    'update_post_term_cache' => false,
    'update_post_meta_cache' => false
);
$querya = new wp_query($argss); 
if ($querya->have_posts()) :
    $row_count=0; 
while ($querya->have_posts()) :
    $row_count++;
    $querya->the_post();
    global $product;
    the_title(); ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>

I have also tried adding new page with page-template (same code), URL:

http://www.v-tac.hr/rezultati-pretrazivanja/

My wp-config.php file has got debug on - debug.log file is empty, no PHP errors are shown up.:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('SAVEQUERIES', true);
define('WP_DEBUG_DISPLAY', true);

Any suggestions? Ideas?

Thanks!

  • Where is $s value coming from? Should that be $search? in this line $mySearch =& new WP_Query("s=$s&showposts=-1"); – BillK Mar 6 '16 at 0:27
  • Did you create child theme to contain your edited templates? – BillK Mar 6 '16 at 0:33
  • @BillK You mean am, my theme contains woocommerce folder with some of .php files copied and modified from woocommerce plugin templates folder? -Yes. – Fritex Mar 6 '16 at 0:35
  • @BillK I have changed $s to $search still the same – Fritex Mar 6 '16 at 0:37
  • 1
    Have you try to print_r( $querya ) ? and how the result? – Jevuska Mar 6 '16 at 3:33
1

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 of posts_per_page, so use that instead. showposts is still valid, but it is converted into posts_per_page in the WP_Query class

  • You shouldn't be setting cache_results to false. Because you will be making use of template tags (like the_title() and the_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 set cache_results to false.

  • Before you set update_post_term_cache and update_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 by the_post_thumbnail()) uses the custom field data stored in _thumbnail_id to get the post thumbnail of the current post. If update_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, and update_post_term_cache is set to false, you are going to run 25 db queries more per page load

    So 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, not wp_query. In the first loop, you are using WP_Query and the next one wp_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

  • Worked like charm. Thank You fore detail informations! Just need to configure it now and upload on server ;) – Fritex Mar 6 '16 at 19:14
  • 1
    My pleasure, glad it worked. Enjoy ;-) – Pieter Goosen Mar 6 '16 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.