[wp-hackers] Parent/child relationship between two different hierarchical CPTs

Mike Schinkel mikeschinkel at newclarity.net
Tue Nov 8 03:29:36 UTC 2011

On Nov 7, 2011, at 9:14 PM, Matthew McGarity wrote:
> 1) If two different Parks have two different Features with the same title,
> WordPress is appending the number to keep the slugs unique.  Not a huge
> issue, and one we can work around.

That should not be happening if you have hierarchical post types AND your post_parent is non-zero.  Is there any chance you set the post_parent AFTER you insert the new record?

> 2) Before the parent/child relationship is set, WordPress will calculate
> the post link after tabbing out of the Post Title field, and such links
> look like http://example.com/parks///.  I suppose this could be gotten
> around by add conditional logic to the if-statement checking for post_type
> in the post_type_link call.

Well.... yeah, I kinda hoped you'd not ask about that one. :-)

Anyway, you have to pay attention to the 4th parameter named '$sample' in the 'post_type_link' hook.  See my updated example: 


> 3) Published L2 and L3 posts without parents repeat the post_name within
> their links. Example: http://example.com/parks/half-dome/half-dome/.  Easy
> enough to add a pre-publish check or include a new post_type_link call
> based on the condition.

I might have solved that, not sure.  OTOH, shouldn't you always ensure you have parent posts anyway?

> Question: how would I extend this into greater depth, such as adding an
> L3?  Ex: Features might have details that go with them (
> http://example.com/parks/yellowstone/old-faithful/events/).  I hacked
> around with the code, and I suspect it's all fine except for the regex,
> which does not appear to be matching (and I suck at regex).  Here's my
> snippet, which also includes the post_parent boxes needed to build the
> relationships:
> https://gist.github.com/1346808

Ouch, that's more then I've can tackle in my free time right now.  It's definitely doable, but will probably take a hour or two to debug and get all working.  Can I recommend using PhpStorm+ZendDebugger? Using such a debugger will help you be able to see what works and what does not.

BTW, yes, it seems you ARE saving the parent ID too late; it's an easy fix though; in your metabox_display() function use "parent_id" for the <select> name and id instead of "mmc_post_parent" and WordPress will just save it for you.

Also, you really should encapsulate your metabox_create() hook function inside the class like I did with my hooks so your function names don't conflict with those in a plugin.  Add your add_action() call to the on_load() method, and be sure to make metabox_create() and metabox_display()  static functions inside the class; just follow the example set in my code.

Hope this helped. Good luck with getting the more complicated use-case working. 


More information about the wp-hackers mailing list