I still haven't found the reason why I was getting different results between the two searches, but I decided I want to go with the permalink search option and just adjust that to my needs.
Here's the simplified snippet that helped me filter the search results:
function define_search_permissions() {
global $wp_post_types;
$wp_post_types['page']->exclude_from_search = true;
$wp_post_types['custom_post_type1']->exclude_from_search = true;
}
add_action('init', 'define_search_permissions');
I prefer to exclude them from here, instead of doing it straight from register_post_type
, since these custom post types should be searchable from a restricted members' area.
A little bit off-topic: There was an extra layer of complexity to the issue, since I'm working on having a bilingual blog, using qtranslate with url pre-path mode active (i.e. turning links into example.com/en/blog
and so on). I managed to force regular searches with the query var attached to redirect to the pretty permalink version by using a htaccess redirect. It might not be the neatest option, but it'll do for now. Here's the two lines of code that do this redirection:
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^(ro|en)/blog/$ /$1/search/%1? [R=301,L]
Should you not use any multilingual plugin that changes your urls, the code simplifies to:
RewriteCond %{QUERY_STRING} ^s=(.*)$
RewriteRule ^blog/$ /search/%1? [R=301,L]
The last ?
on the RewriteRule
statement is left there intentionally. Without it, your links would be example.com/search/findme?s=findme
instead of example.com/search/findme
.