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 have 3 custom searches created using different templates.

The search results page loads different results depending on the referrer:

<?
/* Template Name: Search Results */
$search_refer = $_GET["post_type"];

if ($search_refer == 'post') { 
    load_template(TEMPLATEPATH . '/search/filme.php'); 
}
elseif ($search_refer == 'persoane') { 
    load_template(TEMPLATEPATH . '/search/actori.php'); 
}
elseif ($search_refer == 'trailers') { 
    load_template(TEMPLATEPATH . '/search/trailers.php'); 
}
?>

I'd like to make the url of this page prettier. Right now it looks like this:

/?s=After+Earth&post_type=post

but I'd like it to look like this:

/search/After+Earth/post_type/post

How do I make the url more beautiful?

share|improve this question
    
This one may be helpful: wordpress.org/extend/plugins/monkeyman-rewrite-analyzer –  brasofilo Jan 25 '13 at 6:52

2 Answers 2

This snippet should solve your problem. Just put it into your functions.php file.

function my_insert_rewrite_rules( $rules ) {
    $newrules = array();
    $newrules['search/([^/]+)/post_type/([^/]+)/?$'] = 'index.php?s=$matches[1]&post_type=$matches[2]';
    return $newrules + $rules;
}
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );


function my_flush_rewrite_rules() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_flush_rewrite_rules' );

You will have to flush rewrite rules after adding this code, before it will start to work. Just switch theme to another and back or go to Settings->Permalink Settings and push "Save" button.

share|improve this answer
    
this code don't works –  Batman Jun 21 '13 at 21:31
    
Well, this one I checked, so I'm pretty sure it does, actually ;) You just have to be sure that rewrite rules are flushed correctly after adding it to functions.php file. Then you can go to yoursite.com/search/My+Query/post_type/post/ and it will return search results. –  Krzysiek Dróżdż Jun 21 '13 at 21:44
    
How i did, i upload to my function, i go to Permanent Link's click Save Changes, and i search another term. Shoud be fine but i retrive same... /?s=Movies&post_type=post –  Batman Jun 21 '13 at 21:50
    
This code won't change your search form magically. It only assures that when you type in this url, WordPress will run appropriate search. So when you type this /search/After+Earth/post_type/post in your browsers address bar, the search for posts with "After Earth" will bu called. I have no idea how are you creating search urls (with a form, some links, or any other method) so I'm unable to help you with that part. –  Krzysiek Dróżdż Jun 21 '13 at 21:56
    

first of all ,see this url http://codex.wordpress.org/Using_Permalinks ,and then goo to the admin dashboard Settings->Permalink Settings->Common Settings set the the url as you want,Have a try,just,may be it will help!

share|improve this answer
    
i know of that, the permanent link's are enabe "/%postname%/" –  Batman Jan 6 '13 at 17:08
    
Well, I don't think this would help. You can't set custom rewrites in settings part of dashboard. –  Krzysiek Dróżdż Jun 21 '13 at 20:27

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.