This code enables you to exclude Simple URL’s from the search results page in WordPress.
Simple URL’s is a plugin used to redirect, customise and track links. The links are included in the list of search results when you use the wordPress search form which might not be want you and your sites users want.
Note : You can use the code to exclude any custom post type from WordPress search results pages.
By default, the the Simple URL’s plugin uses a custom post type named surl
which you can see on line 47 of simple-urls > plugin.php.
The plugin also sets the public parameter to true which you can see on line 85 of simple-urls > plugin.php.
'public' => true,
There’s no filter in the Simple URL’s plugin.php file to change this so you need to find another solution.
A common solution is to use pre_get_posts like this:
add_action('pre_get_posts','search_filter');
function search_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search) {
$query->set('post_type', 'post');
}
}
}
The above code will also remove all custom post types from search and only display the default post type in search results unless you add back other post types.
Here’s another solution for logged in members which enables you to exclude 1 specific post type:
Was This Tutorial Helpful?