It’s fairly common to find yourself on a situation where you want to use a specific URL to show a custom content (perhaps something an archive page with two different custom post types), and think: “well, that’s easy. I’ll just create a page to register the URL and a custom page template where I’ll query the contents I need”.

Since we’re adding new rewrite rules, we must call flush_rewrite_rules(), which it’s something you shouldn’t be doing on every page load, so we need to hook into an event that’s not triggered with every request. To achieve this, we will wrap the code as a plugin and call flush_rewrite_rules() on plugin activation:

Taking care of pagination

If you have lots of entries it’s quite likely you’ll want to use some sort of pagination on your custom template, but the rewrite rule we previously added won’t work if you just add /page/2/ to the URL, which means that we need to add a new rule for these cases.