add_rewrite_rule not loading correct page nor getting variables

I’ve trying to get a showreel title and video id into an URL that can then be accessed by the page.

i have the two in the same URL and it either doesn’t work or only one comes in. Currently the page isn’t being found ? (i am constantly flushing the permalinks)

Url i want to get variables from will look like
directors/test-director/showreel/showreel-name/video/111/

The site currently sits in a testing folder called independent_02 but not sure if that affects anything?

here’s the code;

function wpse13483_init() {

    add_rewrite_rule( 'directors/(.+?)/showreels/([^/]+)?/?$', 'index.php?category_name=$matches[1]&showreel=a', 'top' );
    add_rewrite_rule( 'directors/(.+?)/showreels/([^/]+)/video/([^/]+)?/?$', 'index.php?category_name=$matches[1]&showreel=c1&video=c2', 'top' );
    // directors is a custom post_type and should go to single-directors.php

    add_rewrite_rule( 'showreels/(.+?)/video/([^/]+)?/?$', 'index.php?category_name=$matches[1]&video=b', 'top' );
    // showreels is a custom post_type and should go to single-showreels.php

}
add_action( 'init', 'wpse13483_init' );

Any help appreciated!


UPDATES – this rule is now mostly working and catching the variables. However it is going to archive.php and not directors-single.php?

add_rewrite_rule( 'directors/(.+?)/showreels/([^/]*)/video/([^/]*)/?', 'index.php?post_type=directors&showreel=$matches[2]&video=$matches[3]', 'top' );
// directors is a custom post_type and should go to single-directors.php

i think its to do with the ‘?post_type=directors’ but can’t find any reference of what this should be to load single.php

UPDATE 2
Looking at Rewrite analyzeri think its missing the post_type variable?
rules

3 Answers
3

This seems to work!

?post_type=directors&name=$matches[1] seems to be the key

add_rewrite_rule( 'directors/([^/]*)/showreels/([^/]*)/video/([^/]*)/?', 'index.php?post_type=directors&name=$matches[1]&showreel=$matches[2]&video=$matches[3]', 'top' );

so final code now;

function wpse13483_init() {

    add_rewrite_rule( 'directors/([^/]*)/showreels/([^/]*)/video/([^/]*)/?', 'index.php?post_type=directors&name=$matches[1]&showreel=$matches[2]&video=$matches[3]', 'top' );

}
add_action( 'init', 'wpse13483_init' );

function wpa52794_query_vars( $vars) {
    $vars[] = 'video';
    $vars[] = 'showreel';

    return $vars;
}
add_filter( 'query_vars', 'wpa52794_query_vars' );

Leave a Comment