1

hello trying to add rewrite rule to default post type with this

add_action('init', 'add_my_rewrite');
function add_my_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->add_rule('(.*)/server/([^/]+)','index.php?p=$matches[1]&server=$matches[2]','top');
    $wp_rewrite->flush_rules(false);
}
function add_query_vars_filter($vars){
  $vars[] = "server";
  return $vars;
}
add_filter('query_vars','add_query_vars_filter');

but doesn't work let's say my post url is http://localhost/wordpress/movie-slug/ i want the url to be like that http://localhost/wordpress/movie-slug/server/1 with adding the parameter server to the url how can i do that thanks

1

The p query var expects a post ID, use name instead. Also note that rules should only be flushed when they change, as it's a computationally expensive operation. It's best to do that on theme change or plugin activation, whichever is appropriate for where your code is located. You can also do this by just visiting the Settings > Permalinks page in admin.

add_action('init', 'add_my_rewrite');
function add_my_rewrite() {
    add_rewrite_rule(
        '^(.*)/server/([^/]+)',
        'index.php?name=$matches[1]&server=$matches[2]',
        'top'
    );
}
function add_query_vars_filter($vars){
    $vars[] = "server";
    return $vars;
}
add_filter('query_vars','add_query_vars_filter');

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.