|

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:

Similar Posts

  • | |

    QT: Manage Your Blackberry Email Online

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!This is for all of use Blackberry (possibly only on Verizon Wireless) users. The Blackberry Web Panel allows you to manage…

  • |

    It’s Official! WordCamp Scranton is July 18th!

    I wrote this on the WordCamp Scranton blog originally. I am so excited to be organizing this event. The team and I are bound to make sure it’s a great event! After a few months of initial planning and venue hunting, we are happy announce that WordCamp is coming to Scranton, PA on July 18th! It will…

  • |

    Announcing Responsive Design with WordPress

    If you’ve been milling around the site lately, or follow me on Facebook, Twitter, Google+, LinkedIn, or Dribbble, you’ll know I’ve been working on a book. Last week I officially launched the site for @NEPABlogCon and I’m happy today to officially announce my second book, Responsive Design with WordPress. Responsive Design with WordPress shows readers Responsive…

  • Use iGoogle’s GMail Gadget when GMail is Down

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!Gmail is down! Oh No! How will we go on?! So I’m being a bit over-dramatic, but many of us depend…

  • | |

    Include a Sidebar with a Shortcode in WordPress

    Recently I was faced with a pretty interesting problem where I wanted to have a page that was structured: Content – Widgets – Content. Instead of hacking together some sort of Content – Sidebar – Metadata on the backend, I decided to make sidebar widgets accessible via a shortcode you can use in the WordPress…

  • |

    How to Improve Performance on Your WordPress Site

    Did you know that 80% of website visitors will abandon your website if it takes more than 5 seconds to load? That’s a lot of lost revenue and opportunities. WordPress gets a bad wrap for for being slow, but it’s not usually WordPress’ fault! If you use the right tools, you can have a nice,…

3 Comments

  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.

Comments are closed.