Rewrite rule for custom taxonomy

I came up with this with the add_rewrite_rule():

add_rewrite_rule('^recipes-with-([^/]*)/?','index.php?ingredient=$matches[1]','top');

I did some testing for the above, and it works well when you use it for one taxonomy at time. Here is the code from my functions.php:

add_action( 'init', 'create_ingredient_tax' );
function create_ingredient_tax() {
    register_taxonomy( 
            'ingredient', 
            'post', 
             array( 'label' => 'Ingredient', 
            'hierarchical' => true
            ), 
            array( 'rewrite' => array (
                        'slug'=>'recipes-with'
                    ))
        );
}
// Remember to flush_rewrite_rules(); or visit WordPress permalink structure settings page
add_rewrite_rule('^recipes-with-([^/]*)/?','index.php?ingredient=$matches[1]','top');

I then used taxonomy-post_format.php template fiel from WordPress twentyfourteen theme to test that this works. I also flushed rewrite rules for the new rule to become effective.

Leave a Comment