Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm running WordPress 3.3.2 with my own custom-built theme that has 4 custom post types. Currently, I'm looking for a way to replace the search link from example.com/blog/?s=findme with example.com/search/findme.

I've seen that WordPress already has the possibility to generate search results by using the pretty permalink search, but the result it gives includes all the other custom post types as well, while the standard search using the ?s= query var retrieves only blog posts.

How do I limit the search results generated by the permalink search to only include regular blog posts and filter out the custom post types?

share|improve this question
    
Interesting, it should be identical... Tried disabling plugins and using default theme? –  Rarst May 24 '12 at 12:35
    
Yep. Disabling plugins doesn't change anything. Using the default theme kinda fails the purpose, since all the custom post types are defined within my developing theme. –  andreiio May 24 '12 at 14:10

2 Answers 2

If you don't want to search custom post types at all, you can add

'exclude_from_search' => true

to your registration args.

share|improve this answer
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.