Title: Help With Search
Last modified: August 19, 2016

---

# Help With Search

 *  Resolved [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/)
 * 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)

 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677316)
 * As WP search works – the search results page is a multipost view (using search.
   php or index.php, according to the [Template_Hierarchy](http://codex.wordpress.org/Template_Hierarchy)),
   so it never goes to a single post view.
 *  Thread Starter [Tom Morton](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677320)
 * 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.
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677321)
 * 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](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677325)
 * 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!
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677327)
 * It works for me on a test install – thanks, Adam!
 * I added it into the `<head>` section, not in the body!
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677330)
 * 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.
 * [http://adambrown.info/b/widgets/2008/01/08/im-feeling-lucky/](http://adambrown.info/b/widgets/2008/01/08/im-feeling-lucky/)
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677331)
 * So, is it a plugin or a widget? Or works in both ways…
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677333)
 * 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.
 *  [moshu](https://wordpress.org/support/users/moshu/)
 * (@moshu)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677334)
 * 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](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677339)
 * 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?
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677342)
 * 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](https://wordpress.org/support/users/tm3909/)
 * (@tm3909)
 * [18 years, 4 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677346)
 * Alright, well I’ll keep playing around with it. Thanks again, you solved a HUGE
   problem for me.
 *  [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * (@adamrbrown)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677618)
 * I found the problem. Try this version of the plugin:
 * [http://adambrown.info/b/widgets/2008/01/21/im-feeling-lucky-update/](http://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.

 * 13 replies
 * 3 participants
 * Last reply from: [Adam Brown](https://wordpress.org/support/users/adamrbrown/)
 * Last activity: [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-search-1/#post-677618)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
