Skip to content
OSULZER.AT
Code1 min read· updated July 26, 2022

Disable Media in WordPress Search Results

A handy snippet that removes files from your media library from the general search on your website.

The following handy snippet lets you remove files from your media library from the general search on your website. You can add the snippet to your plugin as usual, or to a solution such as the “Snippets” plugin.

add_action( 'init', 'osu_exclude_images_from_search_results' );

function osu_exclude_images_from_search_results() {
	global $wp_post_types;

	$wp_post_types['attachment']->exclude_from_search = true;
}

PHP Freelancer

Prefer to have it done for you?

Customizations, plugins, and integrations - cleanly implemented, €120/h plus VAT.

See the PHP freelancer

New project?

Contact.

Tell me briefly about your project - I'll get back to you personally.

Your data is used solely to handle your inquiry. More in the privacy policy.