4

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

2
  • your rules are a bit confusing - why are you setting category_name in each and not your custom post type? why is showreel set to static values?
    – Milo
    Commented May 21, 2012 at 19:39
  • I got the code from a previous site i did that re-ordered a category by date & desc asc. What should replace the category_name? I guess I'm asking for general guidance for these rewrites and how to structure the rules...
    – v3nt
    Commented May 22, 2012 at 9:27

3 Answers 3

2

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' );
2

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.

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

    return $vars;
}

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.

5
  • Thanks EAMann - but the page is still not found so can't tell if its now catching the vars.
    – v3nt
    Commented May 21, 2012 at 15:58
  • Do some debugging ... global $wp_query; echo(print_r($wp_query)); and see what's actually contained in the query. You should see your variables listed in the $wp_query->query_vars array.
    – EAMann
    Commented May 21, 2012 at 16:00
  • Cheers EAMann - i can see the variables coming through now but its loading archive.php and not single.php... I've a feeling post_type=directors should be something else.
    – v3nt
    Commented May 22, 2012 at 9:45
  • Are you passing some kind of slug or id to WP's internal query processors? If WP doesn't know what post you're looking for, it will return the archive page for that category by default.
    – EAMann
    Commented May 22, 2012 at 14:19
  • i thought that would still be happening automatically? Or it will be set here index.php?SOMETHING=VALUE?... but can't find any reference. It makes sense that post_type=directors is bringing up an archive of that post type but ?postname, ?id or ?post_name doesn't work and i end up on the index page.
    – v3nt
    Commented May 22, 2012 at 14:26
0

In addition to @EAMann answer I can say that you need to flush rewrite rules.

@EAMann answer:

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

    return $vars;
}

Flushing rewriting rules:

function myplugin_activation() {
    flush_rewrite_rules();
}
register_activation_hook ( __FILE__, 'myplugin_activation' );

function myplugin_deactivation() {
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'myplugin_deactivation' );
1
  • Hey Eugene - i am constantly flushing the permalinks via the permalinks page. best, Dan.
    – v3nt
    Commented May 28, 2012 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.