I am trying to create a URL rewrite in wordpress. I am creating a search from through wordpress.
The URL structure I currently have is the following: http://localhost:8888/?post_type=CustomPost&s=searchTerm
I want to achieve the URL structure as follows: http://localhost:8888/CustomPost/search/searchTerm
I added the following rewrite rule:
function search_rewrite_tag() {
add_rewrite_tag('%s%','([^&]+)');
}
function search_rewrite_rule( $wp_rewrite ) {
$new_rules = array(
'^CustomPost/search/([^/]*)/?$' => 'index.php?post_type=CustomPost&s='. $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'init', 'search_rewrite_tag');
add_filter( 'generate_rewrite_rules', 'search_rewrite_rule' );
If I type in this URL: http://localhost:8888/CustomPost/search/searchTerm it works perfectly fine, however if I search from my form, the URL generated is still http://localhost:8888/?post_type=CustomPost&s=searchTerm
How Can I make it automatically rewrite?