I'm using WordPress 3.0.1 and I have made custom post-type called 'dienasgramatas'. My plugin also makes custom rewrite rules for displaying all posts from this post type and even custom permalink structure to query posts with this post type coming from defined author, but I can't create rule for pagination:

This is the one that works:

$new_rules['dienasgramatas' . '/([^/]+)/?$'] = 
  'index.php?post_type=dienasgramata&author_name=' . $wp_rewrite->preg_index(1);

It gives this rewrite rule:

[dienasgramatas/([^/]+)/?$] => index.php?post_type=dienasgramata&author_name=$matches[1]

But this one:

$new_rules['dienasgramatas' . '/([^/]+)/page/?([0-9]{1,})/?$'] = 
  'index.php?post_type=dienasgramata&author_name=' . $wp_rewrite->preg_index(1). 
  '&paged=' . $wp_rewrite->preg_index(2);

Outputs this (faulty) rewrite rule:

[dienasgramatas/([^/]+)/page/?([0-9]{1,})/?$] => 

As you can see, post_type is ignored and this rewrite rule does not work as it should.

Can someone tell me why it's not working or how to make it right?

filter for funcion:

add_filter('generate_rewrite_rules', 'add_rewrite_rules');

Full function is as follows:

function add_rewrite_rules($wp_rewrite) {

$new_rules['dienasgramatas' . '/([^/]+)/?$'] = 'index.php?post_type=dienasgramata&author_name=' . $wp_rewrite->preg_index(1);
$new_rules['dienasgramatas' . '/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?author_name=' . $wp_rewrite->preg_index(1) . '&paged=' . $wp_rewrite->preg_index(2) . '&post_type=dienasgramata';

$wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
return $wp_rewrite;


Basic need is that i want to go to


and get posts by 'admin' with post_type = 'dienasgramata' and second page (e.g. 10 post offset). Rewrite rule without page/2 works fine. Maybe it's a regex issue or query vars issue or how rewrite rules are assigned.

live site example: http://dev.fiicha.lv/jb/dienasgramatas/juris/ - works ok http://dev.fiicha.lv/jb/dienasgramatas/juris/page/2/ - does not work

at the bottom there are all rewrite rules and query vars

  • @banesto - I need more context to be able to answer your questions. What hooks are you using? Are you using the function add_rewrite_rule()? If you can post a complete example it will be easier to help you. Oct 23, 2010 at 9:37
  • @MikeSchinkel - I added whole function with a filter. there are other examples when I would like to get post_type as a variable in rewrites but it gets ignored but i believe its because of the same reason.
    – banesto
    Oct 23, 2010 at 9:51
  • @banesto - Can you also show what URLs structures you are trying to achieve so we don't have to guess and potentially guess wrong? Give some examples please. Oct 23, 2010 at 9:55
  • @MikeSchinkel - I added live examples and explanation
    – banesto
    Oct 23, 2010 at 10:05

1 Answer 1


Hi @banesto:

I think the simple answer may be that you need your paged URL to be specified before your shorter URL, but since I didn't test it I'm not 100% sure.

That said, I've always run into a lot of trouble with the 'generate_rewrite_rules' hook. I'm sure a better developer than me could make it work but for me it's been a lot cleaner to use the add_rewrite_rule() function so that's how I show you have to make your URLs route.

Here's a plugin to do what you asked for (the register_post_type() call is only there to allow this plugin to work in my test install of WordPress; you can replace it with your own):

Plugin Name: Dienasgramatas Urls
Version: 1.0
Author: Mike Schinkel
Author URI: http://mikeschinkel.com/

if (!class_exists('Dienasgramatas_Urls')) {
  class Dienasgramatas_Urls {
    static function init() {
      $post_type = 'dienasgramatas' ;
          'label'           => 'Dienasgramatas',
          'public'          => true,
          'show_ui'         => true,
          'query_var'       => 'dienasgramatas',
          'rewrite'         => array('slug' => 'dienasgramatas'),
          'hierarchical'    => false,
          'supports'        => array('title','editor','custom-fields'),

        "index.php?post_type={$post_type}&author_name=\$matches[1]&paged=\$matches[2]", 'top');

        "index.php?post_type={$post_type}&author_name=\$matches[1]", 'top');

    static function on_load() {
    static function register_activation_hook() {

Of course whenever you add rewrite rules you need to flush the rewrite rules or they simply won't work. Ideally you don't want to flush rules for every page load so the standard way to do it is in a plugin's activation hook. So I created a plugin for you but feel free to just use the add_rewrite_rule() calls in your theme's functions.php file and manually flushing your URLs by clicking "Save Changes" in the "Settings > Permalinks" section of the admin console.


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.