Forums › Forums › Search & Filter Pro › How to have results only show when search form is used?
Tagged: search results
- This topic has 7 replies, 2 voices, and was last updated 5 years, 5 months ago by Anonymous.
-
Anonymous(Private) May 22, 2019 at 9:28 am #211953
Hello.
I have a custom post type list page and have added the search form shortcode to the top.The page currently lists all my custom posts and then filters them accordingly when the search form is used.
My question is: I want my page to initially show no results, so the User has to use the search form to bring back the results.
How do I achieve this?
many thanks,
jeremy.Trevor(Private) May 22, 2019 at 10:23 am #211969I need to know if you have this set as shown here:
https://www.screencast.com/t/jVW9z2FJ
If it is, then the results.php needs to be modified like this (where I have used the ID number of your form):
global $searchandfilter; $sf_current_query = $searchandfilter->get(85204)->current_query(); if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()=="")) { echo '<div>Nothing to see here folks!</div>'; } else { // your current results.php code here }
Make sure any files you modify are in the child theme folder (if you copy our results.php file, it needs to be in a sub-folder of the child theme, called search-filter, not our plugin folder or a parent theme folder). Do NOT edit the copy of results.php that is in our plugin templates folder, as it will be overwritten by updates.
Anonymous(Private) May 22, 2019 at 10:48 am #211977Hi.
OK so originally i had the Search & Filter (none pro ) version installed, so i have just installed the Pro version and started to set up the first form, using all the options etc.On the General Tab I set it to my ‘Lab Test’ custom posts.
Display Results i set it to ‘Post Type Archive’, but i can change that back to ‘Using A Shortcode’ if that is easier? And then on my Archive page add the code you suggested?
My current code is simply this (when i used the none pro version)
<div class=”search”><?php echo do_shortcode( ‘[searchandfilter fields=”search” search_placeholder=”Enter a Batch No” submit_label=”Search” post_types=”,labtests”]’ ); ?></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div><?php the_title(); ?></div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>So should it be set to Use Shortcode, then change the code like this:
<?php
global $searchandfilter;
$sf_current_query = $searchandfilter->get(284)->current_query();
if ((!$sf_current_query->is_filtered())&&($sf_current_query->get_search_term()==””)) {
echo ‘<div>Nothing to see here folks!</div>’;
} else {
if (have_posts()) : while (have_posts()) : the_post();
}?>
Trevor(Private) May 22, 2019 at 11:22 am #211993To not show any results until a search is made requires modification of the PHP template being used by the results page. For any method other than the ‘As a Shortcode’ Display Results method, that would mean modifying a theme template file, which is not within the scope of our support.
Modifying the Shortcode results.php file must be done in the child theme folder, as discussed here:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
The code I gave you would work for this latter method, but I am unsure if it would work if used inside any theme template file.
Anonymous(Private) May 22, 2019 at 1:41 pm #211999Hi Trevor.
thanks for your reply – I now have it working as you instructed. Eg copied the results.php and modified the code as you suggested. And the setting is set to Use Shortcode.The only thing that is not displaying correctly is the submit button wording and the placeholder text.
I currently have it like this in my page:
[searchandfilter id=”284″ fields=”search” search_placeholder=”Enter a Batch No” submit_label=”Search”]
[searchandfilter id=”284″ show=”results”]On the page the input field reads as “Search …” and also the default wording on the Submit button eg “Submit”.
-
AuthorPosts