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'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

share|improve this question
    
@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. –  MikeSchinkel Oct 23 '10 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 '10 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. –  MikeSchinkel Oct 23 '10 at 9:55
    
@MikeSchinkel - I added live examples and explanation –  banesto Oct 23 '10 at 10:05
    
@banesto - Thanks. I've added an answer. :) –  MikeSchinkel Oct 23 '10 at 11:09

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Thank you a lot! A little hassle with singular/plural post_type name and it works: dev.fiicha.lv/jb/dienasgramatas/juris/page/2 –  banesto Oct 23 '10 at 12:00
    
@banesto - Awesome! Glad I could help. –  MikeSchinkel Oct 23 '10 at 20:55

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.