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?
This seems to work!
?post_type=directors&name=$matches[1] seems to be the key
so final code now;
WordPress will strip of query variables that it doesn’t understand by default. So in addition to registering your custom rewrite rules, you also have to register your custom query variables.
Just to further complete the answer, you also need to specify the regular query variables WordPress is expecting. Your original example isn’t passing the
post_type
or anything to identify the post (ID, name, slug, etc) as you’ve already noted in your own comments. Without this, WordPress doesn’t understand how to route the request internally.Kudos for discovering this with a rewrite analyzer. That was indeed the missing piece.
In addition to @EAMann answer I can say that you need to flush rewrite rules.
@EAMann answer:
Flushing rewriting rules: