search

  • Stop Focusing on WordPress Searches and Serve Your Customers

    When my faucet was leaking water into the cabinet below, I had no idea what to do. I didn’t even know the question to ask. So I just googled, “Leaky faucet.” I found some YouTube videos, tried what they recommended, made it worse. Went to Home Depot…twice. They told me “Moen” was good for replacement parts. I ordered parts on Amazon. They didn’t fit. Nothing worked. 4 weeks went by.

    Turned out, until we got 2 different quotes from professionals, we didn’t know the problem was unfixable and it needed to be replaced. Maybe if I had known a bit more about how faucets worked, I would have known what to look for.

  • 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: