Search Using Post ID

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.

Read More

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.

Related posts

Leave a Reply

3 comments

  1. 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.

  2. You can use this

        function my_search_pre_get_posts($query)
    {
    
        // If it's not a search return
        if( !isset( $query->query_vars['s'] ) )
            return;
    
        // If it's a search but there's no prefix, return
        if( '#' != substr( $query->query_vars['s'], 0, 1 ) )
            return;
    
        // Validate the numeric value
        $id = absint( substr( $query->query_vars['s'], 1 ) );
        if( !$id )
            return; // Return if no ID, absint returns 0 for invalid values
    
        // If we reach here, all criteria is fulfilled, unset search and select by ID instead
        unset( $query->query_vars['s'] );
        $query->query_vars['p'] = $id;
    }
    
    // Filter the search page
    add_filter('pre_get_posts', 'my_search_pre_get_posts');
    
  3. 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');