Support Forums

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro How to have results only show when search form is used?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Jeremy Alcock
    #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 Moderator
    #211958

    Are you using our Shortcode Display results method (not to be confused with using the form shortcode)?

    Jeremy Alcock
    #211962

    Hi Trevor.
    Can you point me to the documentation specifically for that? thanks.

    Trevor Moderator
    #211969

    I 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.

    Jeremy Alcock
    #211977

    Hi.
    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 Moderator
    #211993

    To 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.

    Jeremy Alcock
    #211999

    Hi 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”.

    Jeremy Alcock
    #212003

    Forget that, I realised I can do it now in the setting panel of the PRO version.

    Thanks for your help getting this all working though.

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

The topic ‘How to have results only show when search form is used?’ is closed to new replies.