0

EDIT Again, nope it's not impossible, just nasty. Solution below.


Trying very hard to get this working. Have tried numerous suggestions here on stackexchange/overflow

My goals are to simply select a 'page parent' on my custom post type and:

  • Get permalinks displaying nicely, eg: /page/sub-page/sub-customposttype/
  • One custom post type could have a different parent page to another custom post type.
  • Get WordPress menu highlighting working, eg: it will apply an 'active' class to any parent menu items when viewing a child custom post type.
  • (without hardcoding any page ids or custom slugs)

Here is the "Page" that I wish to make as parent: http://ultimateclientmanager.com/support/documentation-wiki/

Here is a "Wiki" custom post type that I wish to nest under that ^ page: http://ultimateclientmanager.com/support/documentation-wiki/change-request/

I have the "Wiki" custom post type slug set to 'support/documentation-wiki' in the code, this allows the permalink URL to look nice like above. However the 'wiki' does not have a parent selected, so menu highlighting does not work (and having different parent page urls for the wiki items would be impossible).

Here's some of my recent attempts on getting it to work.

  1. Set the "wiki" item post_parents to the ID of my "page".
  2. Leave the custom post type rewrite slug set to 'support/documentation-wiki'
  3. This generates a URL like: /support/documentation-wiki/support/documentation-wiki/change-request/
  4. This produces a 404 error.
  5. Debug shows query string of wiki_page=support%2Fdocumentation-wiki%2Fchange-request&post_type=wiki_page&name=support%2Fdocumentation-wiki%2Fchange-request
  6. And a matched rewrite query of wiki_page=support%2Fdocumentation-wiki%2Fchange-request&page=

and:

  1. Set the "wiki" item post_parents to the ID of my "page".
  2. Change the "wiki" post type slug back to just 'wiki' (instead of 'support/documentation-wiki')
  3. This generates a URL like: /wiki/support/documentation-wiki/change-request/
  4. This produces a 404 error.
  5. Debug shows query string of wiki_page=support%2Fdocumentation-wiki%2Fchange-request&post_type=wiki_page&name=support%2Fdocumentation-wiki%2Fchange-request
  6. And a matched rewrite query of wiki_page=support%2Fdocumentation-wiki%2Fchange-request&page=

and a desperate attempt to get the correct URL displaying:

  1. Set the "wiki" item post_parents to the ID of my "page".
  2. Change the "wiki" post type slug back to just 'wiki' (instead of 'support/documentation-wiki')
  3. Hack wp_rewrite during the 'wp' hook, change the struct from '/wiki/%wiki_page%' to just '%wiki_page%' like: like: $wp_rewrite->extra_permastructs['wiki_page']['struct'] = '%wiki_page%';
  4. This generates a correct URL like: /support/documentation-wiki/change-request/
  5. But this also produces a 404 error because there it doesn't match anything in the query: pagename=support%2Fdocumentation-wiki%2Fchange-request&page=

The parent page correctly shows up in the admin "wiki" listing and editing area, just do not seem to be getting anywhere in the frontend.

Current permalink settings are just Day and name http://ultimateclientmanager.com/2013/04/04/sample-post/

This is 3.5.1 setup as multisite.

6
  • Custom Post Type code is here: gist.github.com/dtbaker/5307869
    – dtbaker
    Commented Apr 4, 2013 at 4:39
  • Have you tried making the post type non hierarchical? I've used a parent/child page as slug in the past without issue. To get the proper menu highlighting you can filter nav menu css classes. I wouldn't touch the post parent field, I don't see how that would really be useful anyway.
    – Milo
    Commented Apr 4, 2013 at 5:02
  • Yep non hierarchical didn't help either :( The idea of messing with post_parent is a set of wiki articles can be grouped under different 'pages', and with post_parent set hopefully wordpress would manage all the multi-level menu highlighting without the nav css filter.
    – dtbaker
    Commented Apr 4, 2013 at 5:30
  • I don't think that'll work with different post types. Anyway, I'm on mobile now, will try some things out later and report back.
    – Milo
    Commented Apr 4, 2013 at 5:57
  • I've just had a look at the get_page_by_path() function. Yes it is impossible for wordpress to load a URL that contains nested mixed post types when the post_parent is set (eg: /a-page/another-page/a-custom-post-type ). This function will only return a valid page if all nested URL items are of the same post_type. oh well
    – dtbaker
    Commented Apr 4, 2013 at 14:11

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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