Author url rewrite

Assuming you are using custom post types for songs, books etc:

function add_rewrite_rules($rules) {
    $newrules['author/([^/]+)/songs/?$'] = 'index.php?post_type=songs&author=$matches[1]';
    $newrules['author/([^/]+)/songs/page/?([0-9]{1,})/?$'] = 'index.php?post_type=songs&locations=$matches[1]&paged=$matches[2]';

    $rules = $newrules + $rules;
    return $rules;
}

function flushRules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

/* This function should only really be run once per change of rules - comment out */
add_filter('init','flushRules');

Try the query strings above “index.php?post_type=songs&author=username” and make sure you get the correct post listing you are after on your site (you might need to disable permalinks to test them).

Then you can add the rules in to the function (taking note of the paged rule for each post type).

I am doing exactly this on a live site now, so it is possible – just requires a bit of patience to get the rules correct.

If you are not using custom post types you can change the query strings above from post_type=xxx to taxonomy=tagname or whatever you need to get the listing you want.

Leave a Comment