1

I'm trying to add a new rewrite rule but it doesn't seem to be getting added to the array and it certainly isn't working.

I'm trying to get this URL, dult.dev/lists/games/destiny, to take me from a list view of /lists/games/ (which works just fine) to a single post view of that particular post. My rewrite rule isn't even getting added:

add_action( 'init', 'add_rewrite_tags', 10, 0 );
function add_rewrite_tags() {
    global $wp_rewrite; // Global WP_Rewrite class object
    $wp_rewrite->flush_rules();
    add_rewrite_rule( 
        '^lists/([^/]*)/([^/]*)/?', 
        'index.php/lists=$matches[1]&post=$matches[2]', 
        'top' 
    );
    print_r( $wp_rewrite->rewrite_rules() );
}

When I call the $wp_rewrite->rewrite_rules(), the rule I just made isn't there. I've tried flushing them a handful of different ways, and even tried an extremely simple rule just to see if it was added, and still nothing.

  • what is post? you need to set the correct query vars so WordPress can generate the main query. if you are trying to query a post of a custom type, you need to set name and post_type. – Milo Jun 11 '15 at 5:27
  • Ans what is lists? A custom post type? – cybmeta Jun 11 '15 at 7:08
2

Try this. You also don't want to include $wp_rewrite->flush_rules() inside your function as that would flush on every page load.

add_action('rewrite_rules_array', 'new_rewrite_rules');

function new_rewrite_rules($rules){
        $newrules = array();
        $newrules[ '^lists/([^/]*)/([^/]*)/?' = 'index.php?lists=$matches[1]&post=$matches[2]';
        return $newrules + $rules;
    }

If you want to flush the rules only once then try this:

add_action('init', 'flush_new_rule');

function flush_new_rule(){
   global $wp_rewrite;
   if(is_array(get_option('rewrite_rules')) && !array_key_exists('^lists/([^/]*)/([^/]*)/?$', get_option('rewrite_rules')){
   $wp_rewrite->flush_rules( true );
   };
}
  • thanks david, that seemed to add it to the rewrite rules array, but it still doesnt work. i suspect that is an issue with my regex and url structure... maybe... anyway, thanks for getting me one step closer!! – seth.fansided Jun 11 '15 at 2:58
  • As @cybmeta stated you have regex errors. I touched up the code some, but like others have stated without knowing further details about what "lists" and "post" are then it will be hard to pinpoint the issue. – David Labbe Jun 11 '15 at 11:00
1

You had some errors in the regex that can make the rewrite rules. I don't know what "lists" or what kind of post you want to fetch, so I can not debug that, but this rewrite rule should be correct:

add_action( 'init', 'add_rewrite_tags' );
function add_rewrite_tags() {

    add_rewrite_rule( 
        '^lists/([^/]+)/([^/]+)/?$', 
        'index.php?lists=$matches[1]&post=$matches[2]', 
        'top' 
    );

}

Then you must to flush the rewrite rules; you can do that visiting the permalinks settings page on wp-admin. If you want to flush the rewrite rules within your code, you should do on plugin activation (or theme activation if you are adding the rewrite rules in a theme). For example, in the main file of a plugin:

register_activation_hook( __FILE__, function() {

    flush_rewrite_rules();

} );
register_deactivation_hook( __FILE__, function() {

    flush_rewrite_rules();

} );

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.