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!
1 Answer
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;
}
-
Hi @pl4g4, i got this code from somewhere else and tested. It didn't work. Do you have any other solution?– JornesCommented Apr 2, 2015 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.– pl4g4Commented Apr 2, 2015 at 19:01
-
-
maybe a javascript to check for URLS in the post and remove them.– pl4g4Commented Apr 6, 2015 at 13:36
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