WordPress rewrite rule for custom taxonomies

This makes it possible to have the taxonomy below some post type slug, e.g. the post type is at /cars/ and taxonomies are at /cars/manufacturer/, /cars/year-of-construction/ etc.

You want to register the taxonomy with the same $slug.

<?php

add_action( 'generate_rewrite_rules', 'taxonomy_rewrite' );

function taxonomy_rewrite( $wp_rewrite ) {
  $slug = 'yeah/this/is/what/i/want';
  if ( preg_match( '/\//', $slug ) ) {
    $new_rules = array(
      "$slug/(.+)" => 'index.php?paste_tag=' .
       $wp_rewrite->preg_index( 1 )
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
  }
}
?>

0 comments

Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.