0

How can I display an authors archive by author id instead author slug like

http://example.com/author/author_slug/

to

http://example.com/author/123/
1

You can accomplish this with the following rewrite rule ( you'll want to add this in your functions.php

add_rewrite_rule(
    'author/([0-9]+)/?$',
    'index.php?author=$matches[1]',
    'top'
);

Be aware that you might need to flush your rules for it to become active. You can do this with rewrite plugin.

| improve this answer | |
  • is this work with pagination? – Anjum Aug 16 '13 at 18:03
  • and can i wrap this in a function and hook it in author_rewrite_rules like this // add our custom rewrite rules for user data add_action('author_rewrite_rules', 'my_author_rewrite_rules'); function my_author_rewrite_rules($author_rules) { $author_rules['author/([0-9]+)/?$'] = 'index.php?author=$matches[1]'; return $author_rules; } – Anjum Aug 16 '13 at 18:05
  • with your rewrite rule pagination is not working, giving me 404 error when i am navigating to /page/2/ or so on. – Anjum Aug 16 '13 at 18:26
1

after investigation into wp rewrite rules for author and with help of @Mark Davidson everything is now working perfect author archives pagination and feed rss atom etc.

below is the code which i have created and pasted here to help others, Thanks Mark Davidson for providing a pattern.

// add our custom rewrite rules for author archives
add_action('author_rewrite_rules', 'my_author_rewrite_rules');

function my_author_rewrite_rules() {
    $author_rules['author/([0-9]+)/?$'] = 'index.php?author=$matches[1]';
    $author_rules['author/([0-9]+)/page/?([0-9]{1,})/?$'] = 'index.php?author=$matches[1]&paged=$matches[2]';
    $author_rules['author/([0-9]+)/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?author=$matches[1]&feed=$matches[2]';
    $author_rules['author/([0-9]+)/feed/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?author=$matches[1]&feed=$matches[2]';
    return $author_rules;
}

this function will totally replace the author rewrite rules, if anyone want to add new rules and don't want to replace existing then he/she need to supply $author_rules argument in function like.

function my_author_rewrite_rules($author_rules) {
   // new rules here
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.