I have a question today. I have recently completed a website and there is search box integrated. And, I have a post which has an attachment(attachment's url is www.sample.com/image/sampleimage.jpg) added and no such word "sample" in the body of the post. Today, i noticed that the post can be found when searching "sample" that hit the word in the url. Is there anyway to exclude the attachment url from search query? Any solution? Thanks in advance!

  • can you provide siteurl where you are facing such problem? There may exists sample keyword in your content. I just checked on my local setup with the name of the image which exists in a post, but not result found. So it means that the search already ignore the URL of the media attached with the post – Momin Iqbal May 31 '19 at 4:30

try this to exclude images from search. Add 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;
|improve this answer|||||
  • Hi @pl4g4, i got this code from somewhere else and tested. It didn't work. Do you have any other solution? – Jornes Apr 2 '15 at 18:37
  • try a wordpress plugin to exclude post, pages ?? There is a good plugin. its call search everything. You can teste it out. – pl4g4 Apr 2 '15 at 19:01
  • Is there any other solution without using plugin? – Jornes Apr 3 '15 at 1:25
  • maybe a javascript to check for URLS in the post and remove them. – pl4g4 Apr 6 '15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.