How to Change Author Posts URL

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/

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 2

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
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x