Forums Forums Search & Filter Pro How to stop users from getting *all* posts as Results?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Anonymous
    #252304

    I am using Relevanssi and Search & Filter Pro together. And I’m using my own page and template file to output domain.com/my-search?_sf_s=QUERY

    Everything works great except I just found a little hole… if someone visits domain.com/my-search directly with no query (blank query), then it returns ALL the posts on my site…. how can I instead display the ELSE statement of “No posts match your query” if someone visits that page directly or tries to submit an empty search?

    Trevor
    #252346

    Does that page use a PHP template file? If it does, using a child theme and a copy of that file in the child theme folder, edit that file to place logic like this in to it:

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1234)->current_query();
    if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) {
      echo '<div>Nothing to see here folks!</div>'; 
    } else {
      // the current products output code here
    }

    The ID number must match that of your form.

    Note, if you are using a page builder, this method may not be possible.

    Anonymous
    #252406

    Thank you. I am using a child theme and I’m using /wp-content/themes/mythemename/search-filter/results.php to output the results.

    In results.php at the end I see this:

    }
    else
    {
    	echo "No Results Found";
    }
    ?>

    Do I put your code somehow around that code at the end of the file? How would I add this and where? Thanks for your advice.

    Trevor
    #252410

    In place of this line:

    // the current products output code here

    … you would place the code part of the results.php file, which, by default, would be this part:

    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><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></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
    #252412

    Works perfectly! Great plugin and superb support! Pls close this thread.

    Trevor
    #252416

    Thanks for getting back to me. I will close this thread for now.

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