• Resolved Tom Morton

    (@tm3909)


    I am trying to enable my search bar to take the user directly to a post, not the preview. Example: If a post is titled “1-1-2008”, when they type this in, they are taken directly to the post instead of the preview page that shows the post. I’ve searched everywhere, any help would be greatly appreciated.

Viewing 13 replies - 1 through 13 (of 13 total)
  • As WP search works – the search results page is a multipost view (using search.php or index.php, according to the Template_Hierarchy), so it never goes to a single post view.

    Thread Starter Tom Morton

    (@tm3909)

    Thats correct, but I’m trying to get it to display the first one. All of my posts are a specific item, so none of them have the same title, therefore when a user searches, only one post is displayed on the search page.

    I guess I’m trying to get something like Google’s “I’m Feeling Lucky”, where when you click it, you are taken to the page, not the search results.

    Maybe if you stuck something like this at the very top of your theme’s header.php?

    (Disclaimer: I haven’t tested this–I just pulled it out of my … uh … whatever.)

    <?php
    // I'm feeling lucky!
    global $wp_query; // might not be necessary, don't know
    if (is_search() && 1==$wp_query->post_count){
    	the_post();
    	header("Location: " . get_permalink() );
    	die();
    }
    ?>

    What it does: If it’s a search result page, and there’s only one result, then it redirects you to the result. If there’s more than one result, then it doesn’t do anything–you go to the normal results page.

    Thread Starter Tom Morton

    (@tm3909)

    Edit: Adam, your a genius!

    I really can’t thank you enough, it works perfectly. Instead of sticking it in the header.php, I put it in the index.php. I’ve tested it with several posts and its exactly what I needed. If theres anything I can do (comment in the code or something) let me know.

    Thanks again!

    It works for me on a test install – thanks, Adam!

    I added it into the <head> section, not in the body!

    Now available as a plugin. I recommend using the plugin instead of the code I gave above–it’s more robust, since it combines PHP with JavaScript to make the redirect less likely to fail.

    https://adambrown.info/b/widgets/2008/01/08/im-feeling-lucky/

    So, is it a plugin or a widget? Or works in both ways…

    It’s a plugin, not a widget. The URL is misleading. /b/widgets/ is where I installed my plugins blog, since the first few plugins I wrote were widgets.

    I guess I could move the blog, but I’m too lazy.

    I see. Yes, the url was the “misleading” part. I am perfectly OK with a plugin ?? – not being a widget person.

    Thread Starter Tom Morton

    (@tm3909)

    I feel really stupid, but I took out the code you gave me and installed the plugin, and now its not working…Is there something else I have to do?

    Shouldn’t be. Just paste the code into a file, name it “feeling_lucky.php” or whatever, upload it to wp-content/plugins, and activate it.

    If that doesn’t work… I guess you could go back to the code I gave above.

    Thread Starter Tom Morton

    (@tm3909)

    Alright, well I’ll keep playing around with it. Thanks again, you solved a HUGE problem for me.

    I found the problem. Try this version of the plugin:

    https://adambrown.info/b/widgets/2008/01/21/im-feeling-lucky-update/

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Help With Search’ is closed to new replies.