2

How can I add pages under the URL structure of a Custom Post Type?

For example...

If I had a Custom Post Type called "Knowledge Base" ... Located at: domain.com/knowledge-base/

... and I wanted to add a page called "Archives" ... Located at: domain.com/knowledge-base/archives/

... and I wanted to add sub pages to the archives like ... daily, weekly, monthly, yearly, etc. ... Located at: domain.com/knowledge-base/archives/daily/ (for example).

The only way I can think of doing it is I have to add a page with a slug of "knowledge-base", then add respective sub pages under that.

However, when doing that... The rewrite rules for the page seem to overwrite my post type archive when viewing domain.com/knowledge-base/.

OR

The rewrite rules for the custom post type overwrite the page rewrite rules.

Either way, I'm not able to have a fully functioning custom post type with sub pages appended to it's structure.

How can you add "pages" to your custom post type?

  • You'll have to add your own rewrite rules to handle it. – Milo Dec 23 '14 at 17:30
0

I really should just try to think things out thoroughly before instantly asking questions.

I found a very easy solution. It's not exactly adding "pages" but it does what I need.

When registering the custom post type, there are two things which need to be specified.

By default, the parameter hierarchical is set to false. Solution: set hierarchical => true;

In order for the hierarchical functionality to work, you need to add page-attributes to the supports parameter.

Now, just create a new post in your custom post type called "archives" and select no parent.

Then create another new post, "daily" (for example), and select "archives" as the post parent.

Easy enough.

However, if you have a "page" with a slug of "archives", it seems that your knowledge base "archives" will have a slug of "archives-2" instead of just "archives".

If you encounter that problem, then update to WordPress 4.1. It allows duplicate slugs across multiple post types.

  • Basically, WP stores all pages, posts and post types in one database table so you can't have 2 pages or post types with the same slug as they need to be unique. The best way around this are to use the rewrite function in post type register to put a unique part in the url o – Gareth Gillman Dec 23 '14 at 17:38
  • do they really need to be physical pages? presumably you'll need to create a custom query on these "pages" to get the output you actually want. – Milo Dec 23 '14 at 17:40
  • @Milo Creating a post object for these archives is necessary. It allows my users to add post_content to these "archive pages". So they can describe what the page is about, my plugin then appends archive information to their post_content. – Michael Ecklund Dec 23 '14 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.