I have the following code:

  function ex_rewrite_rules(){
     //discography query variable rewrite
   add_rewrite_rule( 'artists/([^/]+)/discography/?$', 'index.php?album_artist=$matches[1]&discography=yes', 'top');
//albums rewrite rule 
add_rewrite_rule( 'artists/([^/]+)/discography/([^/]+)/?$', 'index.php?album_artist=$matches[1]&discography=yes&albums=$matches[2]', 'top' );
//songs rewrite rule
add_rewrite_rule( 'artists/([^/]+)/discography/([^/]+)/([^/]+)/?$', 'index.php?album_artist=$matches[1]&discography=yes&albums=$matches[2]&songs=$matches[3]', 'top' );
//Function to register query vars
function ex_prefix_artists_query_var($vars){
  $vars[]  = 'discography';
  $vars[]  = 'album_artist';
  return $vars;


//function to load template when discography variable is called
add_filter( 'template_include', function( $path ) {
if( get_query_var( 'discography' ) && is_singular('artists') ) {
    return get_template_directory() . '/single-artists-discography.php';

return $path;

The URL rewrites work perfectly except for discography, which is supposed to load the template 'single-artists-discography.php' when the discography variable is called. Instead, it is going back to index.php.in other words, the url example.com/artists/alicia-keys/discography should load the template, not go back to the site's index.php

What am I doing wrong? Am suspecting it is the url rewrite rule.

Just for extra information, I am attaching a screenshot of the rewrite analyzer, showing that it is constructing correctly rewrite analyzer screenshot for the URL rules detailed above

  • I would guess is_singular('artists') is most suspect. Does your custom rewrite rule results in that conditional? Is it true as expected if you dump it?
    – Rarst
    Nov 5 '14 at 9:00
  • @Rarst is_singular is ok. the problem lies in the rewrite rule in discography
    – nelson
    Nov 5 '14 at 10:01
  • @Rarst just to give a hint, when I type example.com/artists/alicia-keys/?discography=tes , the single-artists-discography.php loads
    – nelson
    Nov 5 '14 at 10:55

in order for the discography rule to work, I changed the rule from:

add_rewrite_rule( 'artists/([^/]+)/discography/?$', 'index.php?album_artist=$matches[1]&discography=yes', 'top');

to this :

add_rewrite_rule( 'artists/([^/]+)/discography/?$', 'index.php?artists=$matches[1]&discography=yes', 'top');

What the original rule was doing was pointing to the Artist's Name and not the Artists custom post type, thus failing to redirect to the template. Thanks a lot to @RachelCarden for this.


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.