4

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?

1

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.

  • 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
0

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!

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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