Pretty permalinks for search results with extra query var

To modify the search rewrite rules you can hook into the search_rewrite_rules filter. You can either add the extra rewrite rules that match post types yourself, or you can change the default “search rewrite structure” to also include the post type and then re-generate the rules (there are four rules: one standard, one with paging and two for feeds). Because WP_Rewrite::generate_rewrite_rules() generates rules at every “directory level”, you will get rules for /search//section/[post_type]/, /search//section/ and /search//. You don’t need the middle rule, but it won’t hurt to keep it in.

add_filter( 'search_rewrite_rules', 'wpse15418_search_rewrite_rules' );
function wpse15418_search_rewrite_rules( $search_rewrite_rules )
{
    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag( '%post_type%', '([^/]+)', 'post_type=" );
    $search_structure = $wp_rewrite->get_search_permastruct();
    return $wp_rewrite->generate_rewrite_rules( $search_structure . "/section/%post_type%', EP_SEARCH );
}

To check the rules, use my Rewrite analyzer plugin.

Leave a Comment