Custom Post Type Rewrite Rule for Author & Paging?

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,})/?$] => 
  index.php?dienasgramata=$matches[1]&paged=$matches[2]

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

www.example.com/dienasgramatas/admin/page/2

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

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):

<?php
/*
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" ;
      register_post_type($post_type,
        array(
          'label'           => 'Dienasgramatas',
          'public'          => true,
          'show_ui'         => true,
          'query_var'       => 'dienasgramatas',
          'rewrite'         => array('slug' => 'dienasgramatas'),
          'hierarchical'    => false,
          'supports'        => array('title','editor','custom-fields'),
        )
      );

      add_rewrite_rule("{$post_type}/([^/]+)/page/?([2-9][0-9]*)",
        "index.php?post_type={$post_type}&author_name=\$matches[1]&paged=\$matches[2]", 'top');

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

    }
    static function on_load() {
      add_action('init',array(__CLASS__,'init'));
    }
    static function register_activation_hook() {
      self::init();
      flush_rewrite_rules(false); 
    }
  }
  Dienasgramatas_Urls::on_load();
}

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.

Leave a Comment