[wp-hackers] Permalinks, Custom Post Types & Pagination

Aaron D. Campbell aaron at xavisys.com
Fri Oct 29 16:50:48 UTC 2010


If you're slug for the custom post type matches the slug for the page 
you're using as the index (list) for the custom post type, there's a 
conflict.  For example, if you use 'beer' as a custom post type, and 
'beer' as the slug for the page that lists them, the url '/beer/page/2' 
is looking for the beer with the slug 'page' rather than passing 'page 
2' to the 'beer' page.  I fixed this by reordering the rewrite rules 
like this:

    add_filter( 'page_rewrite_rules',  array( $this,
    'get_beer_page_rewrite_rules' ) );
    add_filter( 'rewrite_rules_array', array( $this,
    'add_beer_page_rewrite_rules' ) );
    public function get_beer_page_rewrite_rules($rewrites) {
         foreach ( $rewrites as $rule_key => $rule) {
             if ( false !== strpos($rule_key, 'beer') ) {
                 $this->_beer_page_rewrite_rules[$rule_key] = $rule;
                 unset($rewrites[$rule_key]);
             }
         }
         return $rewrites;
    }

    public function add_beer_page_rewrite_rules($rewrites) {
         $rewrites = array_merge($this->_beer_page_rewrite_rules,
    $rewrites);
         return $rewrites;
    }

Basically, get_beer_page_rewrite_rules() takes out the beer page 
rewrites and stores them in a class variable (you can obviously put them 
anywhere, this is a snippet from a class in a plugin I use to run 
http://shouldipoutit.com).  Then add_beer_page_rewrite_rules() adds them 
back at the top of the rewrites, ahead of the custom post type rewrites 
that overlap it.

The advantage is that the slug can match and page number is passed where 
you expect.  The disadvantage is that you can't have a beer with a slug 
'page'.  You can't have everything!

Hopefully that helps anyone else that comes across this issue.

On 10/28/2010 2:18 AM, Olumide Alabi wrote:
> Hi Guys,
> I finally fixed it. It's really funny how a single 's' can mess things up.
>
> I just changed the *query_var* property for the post type from
> press-releases to press-release and flushed my rewrite rules and it worked
> like a charm.
>
> Here's a link if you need to reference the solution.
> http://wordpress.org/support/topic/pagination-for-custom-post-types
>

-- 

Aaron D. Campbell
Owner
*Xavisys <http://xavisys.com>*

Cell: 619-889-3033
Fax: 623-327-0562



More information about the wp-hackers mailing list