Was trying to create author specific page with author specific URL and it was working fine,but when i created this URL did not taken in to account following 2 factors

  1. Author Name Conflict (similar name).
  2. Paging option to be added to author page (for his published posts)

here is my initial rule

function add_my_rule() {    
    global $wp; 
    $wp->add_query_var('args');   
    add_rewrite_rule('writer\/(.*)','index.php?pagename=writer&args=$matches[1]','top'); 
    /*global $wp_rewrite;
    $wp_rewrite->flush_rules();*/
}
add_action('init', 'add_my_rule'); 

This was working file for a URL say

www.myblog.com/writer/umesh-awasthi ,

but there can be more author with same name and this will create a issue.So i was planning to have Blog author URL like SO have

www.myblog.com/writer/001/umesh-awasthi where 001 is user id which will always be unique.

second issue is with paging, as i am able to create paging on the author page but when user is clicking second page the URL is coming as

http://localhost/blog/wordpress/writer/umesh-awasthi/page/2/ so as per my URL-Rewrite rule i will get following data as parameter umesh-awasthi/page/2 which means that now my query will not work as it will expect the author name as umesh-awasthi and will get it as umesh-awasthi/page/2

My Question is can i rewrite the rule so that i should get data in following way in three different variables

  1. userid
  2. author name
  3. page number (if it exits)
    being new to the WP and rewrite i am not sure how i can achieve this
    thanks in advance

1 Answer
1

function add_my_rule() {    
    global $wp; 
    $wp->add_query_var('args');   
    $wp->add_query_var('arg_username');
    add_rewrite_rule('writer/([0-9]+)/([^/]*)/page/([0-9]+)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]&paged=$matches[3]','top');
    add_rewrite_rule('writer/([0-9]+)/([^/]*)','index.php?pagename=writer&args=$matches[1]&arg_username=$matches[2]','top');
    /*global $wp_rewrite;
    $wp_rewrite->flush_rules();*/
}
add_action('init', 'add_my_rule');

This should do the trick. One rewriterule for writer/user_id/username (username isn’t used in the rewriterule but is necessarily to make it work. The second rewriterule is the same except that it adds pagination.


EDIT: added arg_username in code above.

Leave a Reply

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