Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'd like to be able to enter a post ID into the search box in order for the exact post to be returned in the search results. I'd also like to retain the ability to search the posts/page titles and content.

eg, user enters '#123' in the search box, and the search results return just post 123. However, if I were to enter '123' into the search box, it would return any post/page that contains '123' in the content or title.

This article explains how to achieve what i'm after within the Admin - I just need the equivalent for the front-end!

Any help is greatly appreciated, thank you.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

I previously asked and answered my own question about customizing a search. You can find that answer here: The right way to create a custom search page for complex custom post types. It'll be a good read to help you understand the following code.

I'll use the pre_get_posts action to change your search when you are searching for a positive integer. Note that the code I'm providing will not allow you to search for positive integers within your article.

function my_search_pre_get_posts($query)
{
    // Verify that we are on the search page that that this came from the event search form
    if($query->query_vars['s'] != '' && is_search())
    {
        // If "s" is a positive integer, assume post id search and change the search variables
        if(absint($query->query_vars['s']))
        {
            // Set the post id value
            $query->set('p', $query->query_vars['s']);

            // Reset the search value
            $query->set('s', '');
        }
    }
}

// Filter the search page
add_filter('pre_get_posts', 'my_search_pre_get_posts');

If you want to be able to search for ID and a search string, I would recommend including multiple inputs. You can also use other logic within the function provided to make assumptions about what the user is looking for and alter the query variables accordingly.

Note that this code is untested.

share|improve this answer
    
Thanks very much for your response, it works great. Is there any way of maintaining the ability to search for positive integers within the post? If not, would it be possible to create a separate search function purely for searching by post ID? Many thanks for your help. –  Darren Aug 24 '11 at 7:24
    
I would create a checkbox near the search input that says something like "Search for Post ID". If that checkbox is checked, and ID search is performed, if not the normal search is performed. –  tollmanz Aug 24 '11 at 18:17
add comment

This is untested, check the code comments:

function my_search_pre_get_posts( $query ) {
   // Verify that we are on the search page that that this came from the event search form
   if($query->query_vars['s'] != '' && is_search()) {
       // If "s" is a positive integer, assume post id search and change the search variables
       if(absint($query->query_vars['s'])) {
           // Set the post id value
           $query->set('p', $query->query_vars['s']);

           // Reset the search value
           $query->set('s', '');
       }
   }
}

// Filter the search page
add_filter('pre_get_posts', 'my_search_pre_get_posts');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.