Forums › Forums › Search & Filter Pro › How to stop users from getting *all* posts as Results?
- This topic has 5 replies, 2 voices, and was last updated 4 years, 4 months ago by Trevor.
-
Anonymous(Private) July 9, 2020 at 7:23 pm #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(Private) July 10, 2020 at 10:36 am #252346Does 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(Private) July 10, 2020 at 2:39 pm #252406Thank 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(Private) July 10, 2020 at 2:47 pm #252410In 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"; }
-
AuthorPosts