I am currently looking at creating a new type of site archive within WordPress that allows users to filter by category AND by year rather than the default category OR year.

I am hoping to build a sidebar widget that creates the interface but I am looking for ideas and advice on creating the custom permalink setup. Ideally the archive will work with URLs along the lines of:

/category/year/

/category-1/year/

etc.

I have no requirement to filter any deeper than year. I have discovered this snippet of code but it does not work as expected.

Thanks

4 s
4

What you’re looking for is an endpoint. There are already several of these built in: /feed/some-feed/ for feeds or /trackback/ on posts. Those are both endpoints.

Fortunately, WordPress provides a handy function makes adding your own endpionts really easy. add_rewrite_endpoint

This is all the code you need to make your yearly category archives work:

<?php
add_action( 'init', 'wpse31422_init' );
function wpse31422_init()
{
    add_rewrite_endpoint( 'year', EP_CATEGORIES );
}

As a plugin: https://gist.github.com/1296860

It works exactly like Stephen Harris’ answer, and you can reach a yearly, category archive by visiting yoursite.com/category/some-cat-slug/year/2011/.

Adding an endpoint creates a query variable with the same name as the first argument of the add_rewrite_endpoint function. In this case, year. Because this query var already exists in WP (to take care of the data-based archives), we don’t really have to do anything special here.

Leave a Reply

Your email address will not be published. Required fields are marked *