What if you search for content through the search feature on a website, but the results are shown in an unprofessional manner on the search results page. Yes, WordPress by default displays the results of your search query with all the content including content from the shortcode. But sometime it doesn’t look professional and you might want to exclude shortcode content from the results page. For example, a shortcode like [carousel]
can be used to display a carousel, [videos]
can embed a video, and [form]
can insert a contact form.
Whether it’s for privacy reasons, to maintain a cleaner search experience, or for other purposes, WordPress provides methods to exclude shortcode content from appearing in search results.
Excluding Shortcode Content Using Code
To exclude shortcode content from the search results page in WordPress, you’ll need to add the below custom code to your WordPress theme or functions.php
file.
add_filter('posts_where', 'exclude_multiple_shortcodes_from_search');
function exclude_multiple_shortcodes_from_search($where) {
global $wpdb;
if (is_search()) {
$shortcodes = array('carousel','videos','form'); // Add your shortcodes here
foreach ($shortcodes as $shortcode) {
$where .= " AND {$wpdb->posts}.post_content NOT LIKE '%[$shortcode%'";
}
}
return $where;
}