Sorting archive pages with “pretty” URLs?

Adding a rewrite rule with an order part is very easy, if you do it for one site. It would be harder if you want to create a generic solution that works for all installations with all kinds of permalink structures and custom taxonomies.

This short example works on a basic install of WordPress 3.1, with no extra custom taxonomies. I use the orderby prefix to prevent conflicts with existing post names:

add_action( 'init', 'wpse13483_init' );
function wpse13483_init()
{
    add_rewrite_rule( 'category/(.+?)/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?category_name=$matches[1]&paged=$matches[4]&orderby=$matches[2]', 'top' );
    add_rewrite_rule( 'tag/([^/]+)/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?tag=$matches[1]&paged=$matches[4]&orderby=$matches[2]', 'top' );
    add_rewrite_rule( 'type/([^/]+)/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?post_format=$matches[1]&paged=$matches[4]&orderby=$matches[2]', 'top' );
    add_rewrite_rule( 'author/([^/]+)/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?author_name=$matches[1]&paged=$matches[4]&orderby=$matches[2]', 'top' );
    add_rewrite_rule( '([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[6]&orderby=$matches[4]', 'top' );
    add_rewrite_rule( '([0-9]{4})/([0-9]{1,2})/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[5]&orderby=$matches[3]', 'top' );
    add_rewrite_rule( '([0-9]{4})/orderby/([^/]+)(/page/?([0-9]{1,}))?/?$', 'index.php?year=$matches[1]&paged=$matches[4]&orderby=$matches[2]', 'top' );
}

Leave a Comment