How does WordPress choose archive type template?

WordPress uses a Template Hierarchy to determine which template file to load based on the current context:

WordPress Template Hierarchy

This diagram is a visual representation of \wp-includes\template-loader.php, that contains the context-based template-selection logic.

As to your specific questions:

For example, suppose I have two posts by the same author. How do I make WP use the author.php template to show me the posts by that author?

WordPress does this automatically. Assuming you have pretty permalinks enabled, the author archive for a given author is: example.com/author/{author}

For example, I can not read from the chart that if I have a URL on the form example.com/2012 it will use the date.php template. It makes sense, but I can’t see that the URL should be on any form, especially this one.

I think you’re approaching it exactly backwards. Again, if you have pretty permalinks enabled, and are viewing the date-based archives for a given year, month, or day, the URL will be:

  • example.com/{year}
  • example.com/{year}/{month}
  • example.com/{year}/{month}/{day}

But most importantly: none of that has anything directly to do with the template hierarchy. Regardless of the permalink structure, WordPress asks, “is this a date archive?” If yes, then WordPress uses the date-based archive template hierarchy to determine which template file to load.

In general I have a hard time understanding how WordPress selects which template(route) to take.

The Template Hierarchy answers all of these questions. 🙂

I may have misunderstood, but I expect WordPress to determine which template to use before it starts interpreting its contents.

It certainly does.

At that point it can display anything you want depending on its contents and using the is_sometype() functions.

Sure, you can; but if you’re using/loading a more-specific, context-based template file, you don’t need to. For example, if you’re loading date.php, you don’t need to use the is_date() conditional. It’s already known to be true, if that template file is loaded.

What I don’t understand is how it reaches that point. Have I got this reasoning wrong? I’d be very grateful if someone could address this point.

The place to start is the query itself, which contains/defines the current context:

  1. Build the query
  2. Within the query, define current context
  3. Based on current context, determine which template file to load

I have a more-advanced version of the Template Hierarchy diagram, that incorporates/correlates all of the context-based conditionals. It might help with understanding the process.

Edit

To address your additional questions:

Do I understand it correctly that WP parses permalinks using rewrite rules to build the query, or else the query is already in “native” format, i.e. a URL followed by ? and a concatenation of query variables? Can a combination of both occur?

Generally speaking, rewrite rules are a separate (though related) topic. If you have specific questions about rewrite rules, I would search the site, and or ask a separate question.

That said: the rewrite rules, just like the context/template, are derived from the query. WordPress determines the contextually based template whether or not rewrite rules are used.

What is the definition of “current context” with regard to queries? Could you give an example?

Again, you have to go back to the query. The query is an object that determines/defines/holds all of the contextual information. Have a look at the WP_Query() class in source, if you want the gritty details.

Essentially, context is determined by URL query variables. (Note: this is not about pretty permalinks or rewrite rules, which also use the same, underlying URL query variables.) You can see the query variable parsing in action here.

What do the blue .keywords signify that are below each box in Chip’s diagram?

Those are CSS classes added to body_class(), based on context.