url rewriting - Rewrite: WordPress URL rewrite on Search

admin2025-06-03  3

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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748895289a314617.html

最新回复(0)