Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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 May 21 '12 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... –  daniel Crabbe May 22 '12 at 9:27

3 Answers 3

up vote 2 down vote accepted

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' );
share|improve this answer

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.

share|improve this answer
    
Thanks EAMann - but the page is still not found so can't tell if its now catching the vars. –  daniel Crabbe May 21 '12 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 May 21 '12 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. –  daniel Crabbe May 22 '12 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 May 22 '12 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. –  daniel Crabbe May 22 '12 at 14:26

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' );
share|improve this answer
    
Hey Eugene - i am constantly flushing the permalinks via the permalinks page. best, Dan. –  daniel Crabbe May 28 '12 at 13:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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