Forums Forums Search & Filter Pro Display No Result If No Search String Entered

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #219958

    Hi,

    I’m sure this is quite a simple thing that I’m overlooking but I can’t seem to locate the correct setting configuration. I’m running Search and Filter Pro and have a search page using the short code method that is accessed via a “Search” link in my main navigation menu. The problem I have is that when a user clicks this link the search page loads with results already showing which are all a bit random and look odd. I’d just like the page to load with the search box and no results until the user starts entering a search term (the page uses ajax and ajax infinite scroll pagination). I did think it was something to do with the ajax features but disabling them doesn’t make a difference.

    Any help/advice would be appreciated.

    Thanks,

    James

    Anonymous
    #219973

    Hi Trevor,

    Thanks for the prompt reply, I’ve amended my results.php file as per your suggestion on that other post but I think I’ve done something wrong as no results show now.

    This is my code:

    if ( $query->have_posts() )
    {
    	?>
          
    	<?php
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(233)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div>Nothing to see here folks!</div>'; 
    } else {
         
            
    		?>
    	
    	  Results code here.
            
    		<?php} 
    	}
    
    ?>

    Any ideas where I’m going wrong with this?

    Thanks,

    James

    Trevor
    #219982

    That does not look right. It might look like this:

    <?php
    /**
    * Search & Filter Pro
    *
    * Sample Results Template
    *
    * @package Search_Filter
    * @author Ross Morsali
    * @link https://searchandfilter.com
    * @copyright 2018 Search & Filter
    *
    * Note: these templates are not full page templates, rather
    * just an encaspulation of the your results loop which should
    * be inserted in to other pages by using a shortcode – think
    * of it as a template part
    *
    * This template is an absolute base example showing you what
    * you can do, for more customisation see the WordPress docs
    * and using template tags –
    *
    * http://codex.wordpress.org/Template_Tags
    *
    */
    
    if ( $query->have_posts() )
    {
    ?>
    
    Found <?php echo $query->found_posts; ?> Results<br />
    Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    
    <div class="pagination">
    
    <div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    <div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php
    /* example code for using the wp_pagenavi plugin */
    if (function_exists('wp_pagenavi'))
    {
    echo "<br />";
    wp_pagenavi( array( 'query' => $query ) );
    }
    ?>
    </div>
    
    <?php
    while ($query->have_posts())
    {
    $query->the_post();
    
    ?>
    <div>
    <h2>"><?php the_title(); ?></h2>
    
    <p><br /><?php the_excerpt(); ?></p>
    <?php
    if ( has_post_thumbnail() ) {
    echo '<p>';
    the_post_thumbnail("small");
    echo '</p>';
    }
    ?>
    <p><?php the_category(); ?></p>
    <p><?php the_tags(); ?></p>
    <p><small><?php the_date(); ?></small></p>
    
    </div>
    
    <hr />
    <?php
    }
    ?>
    Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
    
    <div class="pagination">
    
    <div class="nav-previous"><?php next_posts_link( 'Older posts', $query->max_num_pages ); ?></div>
    <div class="nav-next"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php
    /* example code for using the wp_pagenavi plugin */
    if (function_exists('wp_pagenavi'))
    {
    echo "<br />";
    wp_pagenavi( array( 'query' => $query ) );
    }
    ?>
    </div>
    <?php
    }
    else
    {
    echo "No Results Found";
    }
    ?>
    Anonymous
    #219986

    Hi Trevor,

    Thanks for the code, just to clarify my code before was working okay it was just when I added the additional lines for the feature that checks if the user has entered anything where it broke. This was my original code:

    <?php
    /**
     * Search & Filter Pro 
     *
     * Sample Results Template
     * 
     * @package   Search_Filter
     * @author    Ross Morsali
     * @link      http://www.designsandcode.com/
     * @copyright 2015 Designs & Code
     * 
     * Note: these templates are not full page templates, rather 
     * just an encaspulation of the your results loop which should
     * be inserted in to other pages by using a shortcode - think 
     * of it as a template part
     * 
     * This template is an absolute base example showing you what
     * you can do, for more customisation see the WordPress docs 
     * and using template tags - 
     * 
     * http://codex.wordpress.org/Template_Tags
     *
     */
    
    if ( $query->have_posts() )
    {
    	?>
          
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    	
    	    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		    <a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>">
    		        <h2><?php the_title(); ?></h2>
                    <?php if( get_field('summary_portfolio_description') ): ?>
                        <p><?php the_field('summary_portfolio_description'); ?></p> 
                    <?php endif; ?>		        
    		       <?php the_excerpt(); ?>
                </a>
            </article>
            
    		<?php
    	}
    
    }
    else
    {
    	echo "<span id='no-results'>No results found, please try altering your search criteria.</span>";
    }
    ?>
    

    I try adding your code above and it shows results but it also shows results when nothing has been entered and the user first lands on the page?

    Does that make sense?

    Thanks,

    James

    Trevor
    #220009

    Is the ID in the example correct to match the ID of your form?

    Anonymous
    #220027

    Hi Trevor,

    If you mean the ID in the code in my post of #219973 then yes that’s correct.

    Kind Regards,

    James

    Trevor
    #220031

    From your original code I would have had this:

    <?php
    /**
     * Search & Filter Pro 
     *
     * Sample Results Template
     * 
     * @package   Search_Filter
     * @author    Ross Morsali
     * @link      http://www.designsandcode.com/
     * @copyright 2015 Designs & Code
     * 
     * Note: these templates are not full page templates, rather 
     * just an encaspulation of the your results loop which should
     * be inserted in to other pages by using a shortcode - think 
     * of it as a template part
     * 
     * This template is an absolute base example showing you what
     * you can do, for more customisation see the WordPress docs 
     * and using template tags - 
     * 
     * http://codex.wordpress.org/Template_Tags
     *
     */
    
    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(233)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div>Nothing to see here folks!</div>'; 
    } else {
    if ( $query->have_posts() )
    {
    	?>
          
    	<?php
    	while ($query->have_posts())
    	{
    		$query->the_post();
    		
    		?>
    	
    	    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		    <a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>">
    		        <h2><?php the_title(); ?></h2>
                    <?php if( get_field('summary_portfolio_description') ): ?>
                        <p><?php the_field('summary_portfolio_description'); ?></p> 
                    <?php endif; ?>		        
    		       <?php the_excerpt(); ?>
                </a>
            </article>
            
    		<?php
    	}
    
    }
    else
    {
    	echo "<span id='no-results'>No results found, please try altering your search criteria.</span>";
    }
    }
    ?>
    Anonymous
    #220035

    Hi Trevor,

    That’s great, works a treat.

    Many thanks for your help.

    James

Viewing 9 posts - 1 through 9 (of 9 total)