Custom Post Type nest under a normal WordPress Page

Figured it out!

It’s a very nasty hack, but I filtered the raw WP ‘query’ so it would support multiple post types nested together in a single URL.

When you load wordpress with a URL like /page/subpage/custom-post-name-here/ it will run this SQL to check if the URL is valid:

SELECT ID, post_name, post_parent, post_type FROM wp_posts WHERE post_name IN ('page','subpage','custom-post-name-here') AND (post_type="wiki_page" OR post_type="attachment")

Of course this query will return no results. So we modify it slightly (nasty!) so it will expand the search on pages and our custom post type:

SELECT ID, post_name, post_parent, post_type FROM wp_posts WHERE post_name IN ('page','subpage','custom-post-name-here') AND (post_type="wiki_page" post_type="page" OR post_type="attachment")

There’s also a few other bits in here to remove the ‘slug’ requirement from a custom post type (which most likely could be done better, still learning WP rewrites!)

Full Code: https://gist.github.com/dtbaker/5311512

Live Example: (‘support’ is a page, ‘documentation-wiki’ is a child of ‘support’, and ‘change-request’ is a custom ‘wiki’ post that is also a child of ‘documentation-wiki’):
http://ultimateclientmanager.com/support/documentation-wiki/change-request/

If anybody can get this same URL structure, menu highlighting and permalink generation working without nasty filtering the WP query I’ll buy them a beer (or three).

Cheers,
Dave