One of my URLs for which I'm wanting to create a Category Archive is coming up with a 404 despite having matching routes and, supposedly, matching templates.

I'm going through the core WordPress code (groan) and haven't figured out where routes are invoked.

It seems that the Categories are decided in query.php in the parse_query function however I can't see where routes are being used to map URLs to files.

  • URLs aren't mapped to files, the decision of what template to load is based on what query vars are populated after the query is parsed. – Milo Jul 21 '15 at 20:07
  • Let's go through that comment - I've just said 'routes are being used to map URLs to files'. You said 'what template to load is based on what query vars are populated after the query is parsed'. These statements are identical. Please re read. If it's not clear, let me re-phrase: 'template to load' == 'file' and 'query vars' == 'URL'. – Snowcrash Jul 21 '15 at 20:17
  • A very nice resource (but may be 1 stage further on from where you are looking) is the template flowchart from here – richardw Jul 21 '15 at 23:51

parse_query is where the majority of the work is done. Query vars and is_ conditionals are set, and template-loader.php just checks those is_ conditional tags to load the appropriate template.

| improve this answer | |

In addition to Milo's answer: In wp::parse_request() the request URI is matched against the rewrite rules stored in $GLOBALS[ 'wp_rewrite' ]->rules. That means, the »route« will be parsed into query variables. The actual matching rule is stored in $GLOBALS[ 'wp_rewrite' ]->matches which is quite helpful for debugging:

add_action( 'wp_footer', function() {
    echo '<pre>';
    var_dump( $GLOBALS[ 'wp_rewrite' ]->rules );
    var_dump( $GLOBALS[ 'wp_rewrite' ]->matches );
    echo '</pre>';
} )
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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