Exclude images (attachments) from search results


Note :This snippet works only with the Classical style option. The Classical style option can be enabled in Appearance > Customize > Advanced > Theme Style. More about the theme style option here


So you’ve set up a really neat search on your Customizr website with the this snippet. It works fine. But when you look at the search results on your site, all you see is screens and screens of images where you were hoping to see your pages and posts.

Here are two ways to exclude image attachments from your search results.

 

Either using Customizr functionality

Add the following code to your functions.php:

 

Or using WordPress functionality

Add the following code to your functions.php:

 

Where to copy/paste this code? => in your functions.php file. We strongly recommend you create a child theme. Download a start-up child theme here.

Remember: you shouldn’t edit the theme’s functions.php.


Related Snippets you may also like


20 thoughts on “Exclude images (attachments) from search results

  • jarek

    Problem with “Exclude images from search results – Customizr” after upgrade 3.4.23 to 3.4.33, snippet generate fatal error as below
    Fatal error: Class ‘TC_post_list’ not found in /public_html/wp-content/themes/customizr_child/functions.php on line 23

    Any idea to update functions.php code?

    DEL
    I found that I had the old snippet

    // Exclude images from search results - Customizr
    add_action('init', 'exclude_images_from_search_results');
    function exclude_images_from_search_results(){
    if ( is_admin() )
    return;
    remove_filter( 'pre_get_posts', array(TC_post_list::$instance,'tc_include_attachments_in_search') );
    }

  • Giorgio25b

    mmmh, not working in Customizr-Pro version 1.0.10

    I also tried:

    but no luck so far…

  • Vlad

    Your code killed my website!!!!!! I can’t log in, and can’t do anything!!!!!! HELP ME!!!!!!!

    Parse error: syntax error, unexpected ‘exclude_images_from_search_res’ (T_STRING) in /home1/sfvlad87/public_html/wp-content/themes/customizr/functions.php on line 66

    after I entered:

    // Exclude images from search results - Customizr
    add_action('init', 'exclude_images_from_search_results');
    function exclude_images_from_search_results(){
    if ( is_admin() )
    return;
    remove_filter( 'pre_get_posts', array(TC_post_list::$instance,'tc_include_attachments_in_search') );
    }

    • ElectricFeet Post author

      Vlad, it looks like something may have changed in the most recent update of Customizr. We’re looking into it.

      In the meantime, to get the site back up, logon using FTP or a file manager if your host provides one and simply delete the code snippet from functions.php. That will get you back up and running immediately.

      Indeed, you may not even need to use FTP, because this function doesn’t fire if you are in the admin screens. So going to yourdomain.com/wp-admin should allow you to see your dashboard. From there you can use Appearance > Editor to remove the snippet from your functions.php.

    • Nicolas

      Hi Vlad, I don’t see any problem in the provided code.
      I should not be related to the update either.
      Try to re-upload a clean functions.php file.
      Cheers

  • Karan Thakural

    Hi Electric feet,

    I’ve used both the codes. But, still i’m getting images. For example, if i search MCB, i’m not getting the image for this product, but, i’m getting the images for the relevant products or its cross-sells. Could you suggest a solution to this.

    Thanks

    • ElectricFeet Post author

      Hi Karan. This code will not eliminate images from your searches when the images are part of a page.

      Maybe it’s best to give an example:

      joe-the-baker.com is a site that uses WordPress and Customizr. If you search for macarons, you get several results shown in http://www.joe-the-baker.com/?s=macarons . Some of those results are for articles on macarons and the articles say “This entry was posted on …“. Other results are for images alone and say “Published [date] at dimensions [dimensions] in Macarons“.

      When you click on the latter links—for the images—all you get is the image (“attachment” in WordPress language). No article; nothing else; just the image, inside a Customizr page.

      The code above stops this happening. Users who search on sites that have applied the above code will only get pages/posts. They will not get images on their own on a page.

      So the code looks to be working on your site. A search throws up any page that has MCB written in it. The WordPress search worked well in that it has given you the MCB page as the first hit. The other pages have MCB written in them. The missing picture on the first article is probably because you haven’t set a featured image for that page. If you set a featured image, it should show up.

      If you have more problems, better to post on the WordPress Customizr forum: http://wordpress.org/support/theme/customizr#postform

Comments are closed.