|

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

  • Speaking: NEPABlogCon and Beyond!

    Though I haven’t officially announced it yet, I have been talking for a while about a new book I’m working on called Responsive Design with WordPress. We are a couple of months out from release but things are going well! In the time running up to the release I’ll be out there speaking quite a bit to…

  • |

    I’m on Github!

    I know it’s something I should have had for a while but I generally don’t feel my code is up to snuff. Seeing as I’ve been promising people my code, however, it makes sense. My username on @github is jcasabona. You can get to my repo using the link below. My Github Repository Right now…

  • |

    Responsive Design with WordPress Giveaway

    As you’ve probably read, my book, Responsive Design with WordPress, came out last month and is now available to purchase online and in stores. My publisher has also sent me a few copies to give to family, friends, important people, etc. I’ve decided that I’m going to give a few of those copies away through a…

  • | |

    2015: Resolving to Do More, and Less

    Every year I publish my list of resolutions as well as a scorecard from the previous year. For 2015, I’ve redesign the site (well, I used HTML5 Boilerplate) and broke my resolutions down into 2 categories: “Do More” and “Do Less.”

  • |

    Security vs. Usability

    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! A problem that all web developers people in the computer field face is security. When creating your application, website, server,…

  • |

    Excerpt from Building WordPress Themes from Scratch

    Recently my first book, Building WordPress Themes from Scratch, made its way back onto the market. In this book, I teach you how to use WordPress as well as how to leverage the API to create your own custom themes, plugins, and content types. Here is an excerpt from the book. Enjoy!

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.