Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code :

global $wp;
        $wp->add_query_var('akey');
        $wp->add_query_var('sort');
        $wp->add_query_var('paged');
        add_rewrite_rule('^activate/([^/]*)/?','index.php?pagename=activate&akey=$matches[1]','top');
        add_rewrite_rule('^users/$','index.php?pagename=users','top');
        add_rewrite_rule('^users/most-active/?','index.php?pagename=users&sort=mostactive','top');
        add_rewrite_rule('^users/most-active/paged/([^/]*)/$','index.php?pagename=users&sort=mostactive&paged=$matches[1]','top');
        add_rewrite_rule('^users/recent/?','index.php?pagename=users&sort=recent','top');
        add_rewrite_rule('^users/recent/paged/([^/]*)/$','index.php?pagename=users&sort=recent&paged=$matches[1]','top');
        add_rewrite_rule('^users/oldest/?','index.php?pagename=users&sort=oldest','top');
        add_rewrite_rule('^users/oldest/paged/([^/]*)/$','index.php?pagename=users&sort=oldest&paged=$matches[1]','top');
        add_rewrite_rule('^users/by-username/?','index.php?pagename=users&sort=byusername','top');
        add_rewrite_rule('^users/by-username/paged/([^/]*)/$','index.php?pagename=users&sort=byusername&paged=$matches[1]','top');

the problem is the following users/recent/ works while users/recent/paged/number/ does not work

Any ideas ?

Thanks

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Your regex is a little flaky - you shouldn't specify the start anchor ^, and some support trailing slashes, others don't - plus I'd opt for a better expression for matching page numbers. And you could certainly trim it down to:

add_rewrite_rule(
    'activate/([^/]*)/?$',
    'index.php?pagename=activate&akey=$matches[1]','top' );

add_rewrite_rule(
    'users/?$',
    'index.php?pagename=users','top' );

add_rewrite_rule(
    'users/(most-active|recent|oldest|by-username)/?$',
    'index.php?pagename=users&sort=$matches[1]','top' );

add_rewrite_rule(
    'users/(most-active|recent|oldest|by-username)/paged/([0-9]{1,})/?$',
    'index.php?pagename=users&sort=$matches[1]&paged=$matches[2]','top' );

I noticed that by-username translates to the sort parameter byusername - if the sorting logic can't be changed to accept the hyphenated value (which I recommend for consistency), add a separate, specific rule-pair.

If you're still having problems, load the request and print out $wp->matched_rule - this'll tell you which rule WordPress hit & proceeded to handle.

share|improve this answer
    
Your answer is great however I have the following problem the script works until you reach paged/2 form paged/2 it adds something like : users/recent/paged/4/page/4/ I don't know where the page/4 comes from –  Paul Dumitru Jul 3 '12 at 17:56
    
Have you flushed your rules? I know you're adding them on-the-fly but it's always worth ruling out (just visit the permalinks settings page to flush). –  TheDeadMedic Jul 3 '12 at 18:45
    
I have done that but still no luck .. –  Paul Dumitru Jul 4 '12 at 12:20
    
What was the result of $wp->matched_rule? –  TheDeadMedic Jul 4 '12 at 16:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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