Support » Plugins » Search function: If only one matching result, go right to that page directly.

  • The subject says it all, say you run a search for “tree” and there is only one matching result instead of showing you the title of the result and an excerpt, how about just direct you right to the full content for that entry?

    Anyone executed this functionality, know of a solution or have thoughts on one? I wanted to poll the community first before attempting to write one.

    If I have to write one I am thinking of using the search counter, and doing something like … in search.php in the header put a redirect condition before any other PHP that says, If search results = 1 …. Redirect to the_permalink

    Just a thought .. not sure it would work.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Can anyone help with this?

    can anyone reply to my post anyone der

    Please, I need an awnser to this problem to. Can anyone help?



    Forum Moderator

    I don’t think this is possible. Nor is it really desirable. Standard behaviour when running any search is to display a listing of results – even if there is only 1 or even none. To change this basic behaviour could create severe problems for some site visitors. Best I can suggest is that you try looking for a plugin that replicates Google’s “I’m Feeling Lucky” functionality. That, at least, should be understood by visitors.

    Okey, but if I realy want to have this function. How would I proceed?



    Forum Moderator

    Try looking for a suitable plugin.

    Yeah, but there are none. So benfranklins question remains.. It can’t be that hard?



    Forum Moderator

    I’m sorry but I cannot magic up a suitable plugin for you. I can only suggest that you try using Google to find an existing plugin or consider building a plugin yourself.

    This is a BIG foum with more users than you (no disrespect). Maybe someone alse can help?

    Wow! Realize I posted this 10 months ago but I did figure out a hack … basically you could just count how many posts in your loop and right a conditional so if there is only one result, just redirect to the_permalink for that one post …

    Looks something like this:

          $i = 0;
    				while (have_posts()) : the_post();
    			<?php if (($count == 1)) { ?>
    <script type="text/javascript"><!--
    window.location = "<? the_permalink(); ?>"
    		<? 	} else { ?>
    <? } ?>

    I don’t use it anymore because I felt like it wasn’t that big of a deal to have users just click on the result even if only one but while I had it running on my site it worked even if it probably is a little ghetto … only solution I came up with.

    Thx Ben!

    Is this the the correct code and where do I insert it? I guess in the search.php somewhere?

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Search function: If only one matching result, go right to that page directly.’ is closed to new replies.