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/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!
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/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!
Share Improve this question asked Apr 2, 2015 at 17:41 JornesJornes 7535 gold badges12 silver badges31 bronze badges 1 |1 Answer
Reset to default 1try 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;
}
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 IqbalAhmed Commented May 31, 2019 at 4:30