Forums Forums Search & Filter Pro Directly displaying result if only one result matches search

Viewing 4 posts - 1 through 4 (of 4 total)
  • Anonymous
    #180513

    Hello,

    is it possible to directly switching to the search result page without showing the results list, if only one result matches the search filter?

    I’m using the shortcode search and don’t want the user need to click on one result in a list, if there is only one result.

    Thanks

    Trevor
    #180534

    It could be done with custom coding. In the results file, you have the number of results.

    You could add a condition that if it is 1, then output a script on to the page (it would need to be inside the while have posts part) like this:

    (This JavaScript example opens the new site in a new browser window after a 4.5-second (4500 ms) delay)

    <script language="javascript" type="text/javascript">
      window.location.replace("<?php the_permalink(); ?>");
    </script>

    This is untested.

    Anonymous
    #180572

    Thanks for your thoughts. I will give it a try.

    Cheers

    Anonymous
    #180641

    For all users got the same idea:

    If you are in your results.php file, just add a counter and a variable for the current permalink right under the query inside the loop:

    
    ...
    $query->the_post();
    $resultcount++;
    $resulturl = get_permalink();
    ...
    

    then you can redirect to the page of the one and only result outside the loop:

    
    <?php if ($resultcount == 1) {
    			echo "<script>parent.self.location='".$resulturl."';</script>";
    		} ?>
    
Viewing 4 posts - 1 through 4 (of 4 total)