Add Attachments to WordPress Search Results

I feel like this has to have been done a lot, and there are great plugins out there for it, but if you’re just looking to add a quick function to your theme (or a really simple plugin) yourself, here’s how to modify WordPress’ search query to include attachments (like images).

function attachment_search( $query ) {
    if ( $query->is_search ) {
       $query->set( 'post_type', array( 'post', 'attachment' ) );
       $query->set( 'post_status', array( 'publish', 'inherit' ) );
   return $query;

add_filter( 'pre_get_posts', 'attachment_search' );

This does 2 things: includes posts of type ‘attachment’ to the search query, and adds the post status of ‘inherit’. This will ensure that any images (or other attachments) that were added while adding a new post or page will be included in the results.

You can also extend the post type array to include your own Custom Post Types (eg array(  'post', 'attachment', 'products' ); )

These 2 posts were very helpful in getting this code together:


  1. Hi, thanks for this tutorial, now I can use attachment in search result.

    I have a question, I’d like to do the same thing but for archive pages (category and tags) but this code doesn’t work, even when I change query->is_archive()

  2. Other programs offer features for example disc creation, technical support and documentation.
    Using the ideal hardware, you can as well record videos in traditional formats,
    such as VCRs and also TV recording. It’s important to bear in mind that bleaching doesn’t
    always result in the whiteness that you expected.

Leave a Reply

Your email address will not be published. Required fields are marked *