I'm working on a category page for a custom post type called articles in WP 3.1, and I'm having some trouble with extracting the page number.

The url: /articles/category/background/

Currently, what works for me, from my functions.php:

add_action( 'init', 'wpse7379_init' );
function wpse7379_init() {

This internally rewrites the url to: /index.php?post_type=articles&category_name=background

I'm trying to make the pagination work by expanding the rewrite rule, but it's not working for me.

The pagination works as follows: /articles/category/background/page/2/

New rule:


This should rewrite the url to: /index.php?post_type=articles&category_name=background&paged=2

I'm using a query with args on my archive-articles.php page, but just a regular loop on the categories.php page, where the posts are displayed.

When I try /index.php?post_type=articles&category_name=background&paged=2 it works, but the url that gets rewritten doesn't. Its just displaying the first page, no matter what I enter as pagenumber.

Does anyone know how to fix this? I think I'm looking for a proper regex, I kinda suck at regexes, so I guess thats the problem.

  • Are you using a custom query or passing args to query_posts?
    – Bainternet
    Feb 12, 2011 at 22:51
  • Added some clarification. I'm using a normal loop on the categories.php page.
    – TomvB
    Feb 13, 2011 at 9:42

1 Answer 1


I see three issues in your rewrite rule.

  • The page number is the third, not the second capture group. You count each opening (, so the first is the category name, the second is /page/[0-9]+, and the third is only the [0-9]+ you need. So change the paged parameter to $matches[3].
  • The page part can be optional, so you need to add a ? at the end (this is the reason we put it in a group).
  • The categories can be hierarchical, like /fruit/banana/. For this reason, you should not match them with [^/]+ (any character except a slash), but .+? (any character, but non greedy, so that the rest of the regex can still match).

Together, this results in the following rewrite rule:


If you don't use it already, I recommend you debug your rules with my rewrite analyzer. You can test them live and see what the query values will be.

  • That works, awesome! I really can't thank you enough for both the solution and the explanation, simply amazing :)
    – TomvB
    Feb 13, 2011 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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