[wp-hackers] Custom post_type archives and wp_rewrite

Lox lox.dev at knc.nc
Thu Mar 24 08:09:06 UTC 2011


Hello,

I have tested the "has_archive" argument to register_post_type and I am not
totally satisfied of the result for a couple of reasons:
- custom post type archive can't be added to a nav menu
- custom post type archive title can't be customized easily

So I went an other way.

Building a plugin for books, in the plugin settings page I allow users to
select what I call a "holding page", a page that holds the custom post type.
Then I use that page's slug for the value of the rewrite parameter of the
custom post type and I had a filter to the_content witch makes a custom
wp_query and calls a custom template to render the custom post type archive,
and I add the result to the holding page's content.

That works pretty well in fact: /subpage/books shows me the books archives
and /subpage/books/my_nice_book shows me the book I need.

But I face a couple issues. Because I use the same rewrite slug as the
page's slug, I have lost the books feed (with anyway points to the books
comments feed page) and I also lost the pagination (books/page/2).

So I thought about adding myself the proper rewrite rules for feeds and
pagination at the top of the other rules, but it fails. To check my rewrite
rules I add a filter:

add_filter('generate_rewrite_rules', array($this, 'generate_rewrite_rules')
);
function generate_rewrite_rules($wp_rewrite) {
    var_export($wp_rewrite->rules);
}

And I properly get at the top of that array:

array (

'books/feed/(feed|rdf|rss|rss2|atom)/?$' =>
'index.php?post_type=books&feed=$matches[1]',
'books/(feed|rdf|rss|rss2|atom)/?$' =>
'index.php?post_type=books&feed=$matches[1]',

But if I go to mysite.com/books/feed, I get a 404 error... (I flushed the
rules)

Any thought, anyone?

-- 
Lox
lox.dev at knc.nc


More information about the wp-hackers mailing list