add_rewrite_rule with 3 or fewer matches?

Well, apparently lots of trial and error pays off!

I used this code instead, where I reversed my add_rewrite_rules and now it works perfectly.

// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
    // The regex to match the incoming URL
    'our-work/our-year/([^/]*)/([^/]*)/([^/]*)/?',
    // The resulting internal URL: `index.php` because we still use WordPress
    // `pagename` because we use this WordPress page
    // `designer_slug` because we assign the first captured regex part to this variable
    'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]&our_work_post_slug=$matches[3]',
    // This is a rather specific URL, so we add it to the top of the list
    // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
    'top' );

// Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
    // The regex to match the incoming URL
    'our-work/our-year/([^/]*)/([^/]*)/?',
    // The resulting internal URL: `index.php` because we still use WordPress
    // `pagename` because we use this WordPress page
    // `designer_slug` because we assign the first captured regex part to this variable
    'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]&our_quarter_tax_slug=$matches[2]',
    // This is a rather specific URL, so we add it to the top of the list
    // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
    'top' );

 // Remember to flush the rules once manually after you added this code!
add_rewrite_rule(
    // The regex to match the incoming URL
    'our-work/our-year/([^/]*)/?',
    // The resulting internal URL: `index.php` because we still use WordPress
    // `pagename` because we use this WordPress page
    // `designer_slug` because we assign the first captured regex part to this variable
    'index.php?pagename=our-work/our-year&our_year_tax_slug=$matches[1]',
    // This is a rather specific URL, so we add it to the top of the list
    // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
    'top' );

I would guess this has something to do with the 'top' parameter on the add_rewrite_rule. Most likely its some kind of other mystical magic I have no idea about. I’d be interested to find out exactly what is going on here though.

Leave a Comment