Forums › Forums › Search & Filter Pro › Directly displaying result if only one result matches search
Tagged: direct, one match, one result
- This topic has 3 replies, 2 voices, and was last updated 7 years, 8 months ago by
Anonymous.
-
Anonymous(Private) June 15, 2018 at 5:54 pm #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(Private) June 16, 2018 at 7:29 am #180534It 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(Private) June 18, 2018 at 4:59 pm #180641For 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>"; } ?> -
AuthorPosts