Can’t create rewrite rule

I try to create rewrite rule for my custom post type. I need it to be something like “/%post_type_name%/%taxonomy_slug/%post_slug%”. Here’s the code I used to add rule:

add_action('init', 'episodes_rewrites_init');

function episodes_rewrites_init(){
    add_rewrite_rule(
        '^episodes/([^/]*)/([^/]*)/?$',
        'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]', 'top'
    );
}

It doesn’t work and i can’t figure out why. Please help.

Related posts

Leave a Reply

2 comments

  1. Well mate, I have never used add_rewrite_rule, WP codex says that is It is most commonly used in conjunction with add_rewrite_tag().
    So just I show you how veterans used to do this rewriting rules in the old times.
    But moreover If you are building a plugin for the masses you must avoid use WP 3.5 features Much people still use WP2.*.

    These set of actions can help you:

    add_action('init', 'flushRewriteRules');
    function flushRewriteRules()
    {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
    
    add_filter('rewrite_rules_array', 'insertMyRewriteRules');
    function insertMyRewriteRules($rules)
    {
        $newrules['^episodes/([^/]*)/([^/]*)/?$'] = 'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]';
        return $newrules + $rules;
    }
    
    add_filter('query_vars', 'insertMyRewriteQueryVars');
    function insertMyRewriteQueryVars($vars)
    {
        array_push($vars, 'episodes', 'seasons', 'post_type');
        return $vars;
    }
    
  2. Well, I’ve just installed plugin called “Custom Post Type Permalinks”, added url template for my custom post like “/%seasons%/%postname%/” and now it rewrites urls like I want. But I still don’t know how to do it in code though…