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:
// Exclude images from search results - Customizr add_action('wp_loaded', 'exclude_images_from_search_results', 100); function exclude_images_from_search_results(){ if ( method_exists('TC_post_list', 'tc_include_attachments_in_search') ) remove_filter( 'pre_get_posts', array(TC_post_list::$instance,'tc_include_attachments_in_search') ); }
Or using WordPress functionality
Add the following code to your functions.php:
// Exclude images from search results - WordPress add_action( 'init', 'exclude_images_from_search_results' ); function exclude_images_from_search_results() { global $wp_post_types; $wp_post_types['attachment']->exclude_from_search = true; }
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.
20 thoughts on “Exclude images (attachments) from search results”
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') );
}
Hi,
Look at the release note of verison 3.4.30: http://presscustomizr.com/customizr-pro-v1-2-30-customizr-free-v3-4-30-release-note/
//class prefixes have been changed from TC_ to CZR_//
If you have used a child theme and used Class TC_post_list, change it to czr_post_list
Hope this helps
mmmh, not working in Customizr-Pro version 1.0.10
I also tried:
but no luck so far…
Hi Giorgio,
tested the first snippet right now and it works.
Your one doesn’t ’cause you use `$this`, which in that context means nothing 😛
Here to see what is `$this` -> http://php.net/manual/en/language.oop5.basic.php
Hope this helps.
Hey Rocco!
It’s not me being wrong, is the world that is spinning too fast and I can’t keep up!
Yes, the snippet works! Works now and worked before.
No, it is me confusing images with thumbnails!
I did a test here Search “Vertical” setting the snippet with
, only then I noticed what’s the difference 🙁
Sorry for the fuzz… jumping on the forum
Eheh, it happens 🙂
Thanks a lot. You save a lot of time!
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') );
}
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.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
Thanks a lot. I got it.
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
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
Hello, the 1st method is breaking WordPress administration. The second one works.
Thanks,
Ok Thanks
Thanks Eloy, we’ve updated it to work with the latest releases. Thanks for pointing it out!
(And thanks Rocco!)
Hello, both methods are still breaking WordPress administration for me.
Hi Amanda,
Read this article: http://www.presscustomizr.com/customizr/how-to-customize-customizr-wordpress-theme/
If you still have problems, head on over to the WordPress forums and post there: http://wordpress.org/support/theme/customizr#postform
Thank you, I no longer have images appearing in my posts.
Great! Glad you got it working 🙂