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'm looking for a way to search through posts by ID, preferably with support for custom post types. I was hoping there'd be a plugin enabling this functionality, but I've failed to find anything. Any ideas would be greatly appreciated, thank you.

share|improve this question
    
Can you explain what you mean search by ID, every post has a unique ID, how could this search possibly yield any more than a single result? –  t31os Feb 15 '11 at 23:41
    
@t31os A single result is what I want :). I am referring to being able to seach posts by ID in the admin backend. If you go to domain.com/wp-admin/edit.php you have a search field at the top, and you can easily locate a post by searching through the post titles. I need to be able to do the same, but using IDs. Say, enter '1343', press 'search', get post #1343. –  pereyra Feb 16 '11 at 2:45
    
how does it work for frontend search? –  chrispo May 3 '12 at 10:07
add comment

1 Answer 1

up vote 6 down vote accepted

Not sure i understand why you'd want to query by ID, but that said it's possible in a hacky kind of way(i like this method because it's simple).

add_action( 'parse_request', 'idsearch' );
function idsearch( $wp ) {
    global $pagenow;

    // If it's not the post listing return
    if( 'edit.php' != $pagenow )
        return;

    // If it's not a search return
    if( !isset( $wp->query_vars['s'] ) )
        return;

    // If it's a search but there's no prefix, return
    if( '#' != substr( $wp->query_vars['s'], 0, 1 ) )
        return;

    // Validate the numeric value
    $id = absint( substr( $wp->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( $wp->query_vars['s'] );
    $wp->query_vars['p'] = $id;
}

All you then do is search using the regular search box using a #(hash) prefix infront of the numeric ID.

#123

..would return the post with an ID of 123.

I'm sure there's more complicated routes that could be taken to do this, but i don't see any issues with this approach, unless you have lots of posts with titles that start with a hash(but you could always swap the hash for another character).

Hope that helps. :)

share|improve this answer
    
@t310s this works, thank you so much! the only (though purely cosmetic) issue I get is that at the top of the search results I'm getting: Search results for “” — while Search results for “#123” would be ideal. Otherwise this is exactly what I needed, thanks again. –  pereyra Feb 16 '11 at 17:41
    
That's an unfortunate side-effect of unsetting the search terms just before the query, but now i've had another look at edit.php i can see an easy fix... :) Update the unset() call to the following.. unset( $wp->query_vars['s'], $_GET['s'] ); and the search term text will disappear when doing the ID search.. p.s. happy to help.. :) –  t31os Feb 16 '11 at 17:48
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.