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

Matthew McGarity matthew at mcgarity.me
Tue Nov 8 02:14:45 UTC 2011


Thanks, Mike -- I was focusing solely on post_type_link() and neglecting to
include the calls to add_rewrite_rule.  For the L1-to-L2 relationship, the
example you provided was clear to follow, and it works with the following
quirks:

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.

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.

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.

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

Thanks!

MMc...

Matthew McGarity
http://mcgarity.me
(972) 275-9673



On Sat, Nov 5, 2011 at 6:54 PM, Mike Schinkel
<mikeschinkel at newclarity.net>wrote:

> Hi Matthew:
>
> You're going to have to get your hands dirtier if you want more complex
> URL routing than /%post_type%/%post_name%/.  :-)
>
> Minimally you'll need to use add_rewrite_rule() in the 'init' hook to tell
> WordPress to route the URL, and also hook both 'request' to validate the
> URL and 'post_type_link' to output the correct link format for the park
> features.  To make it robust you probably also want to put in a plugin so
> you can do your flush_rewrite_rules() in the activation and deactivation
> hooks instead of putting flush_rewrite_rules() in the standard 'init' hooks.
>
> Anyway, it was probably too complicated to explain so I decided to code it
> up for you. You can find a working plugin developed for WP 3.3 here:
>
> https://gist.github.com/1342165
>
> Note I couldn't keep mmc_level1 and mmc_level2 post types straight during
> coding so I renamed them mmm_park and mmc_park_feature in my plugin.
>
> Hope this help.
>
> -Mike
> P.S. Just FYI I believe the WordPress URL routing is too much of a PITA so
> I posted a proof-of-concept for an alternative system on Trac several
> months ago (which admittedly doesn't yet address the more complex parts of
> the example I coded up for you):
>
> http://core.trac.wordpress.org/ticket/18276
>
> FWIW, my opinion regarding WordPress' URL routing is not fully shared by
> everyone on the hackers list.  :-)
>
>
>
> On Nov 4, 2011, at 2:50 PM, Matthew McGarity wrote:
>
> >>
> >> Next question: You say "It doesn't work."  The URL routing?  If yes,
> what
> >> does your reqister_post_type() code look like?
> >>
> >
> > $labels = array(
> > 'name' => _x( 'Parks', 'mmc_level1' ),
> > 'singular_name' => _x( 'Park', 'mmc_level1' ),
> > 'add_new' => _x( 'Add New', 'mmc_level1' ),
> > 'add_new_item' => _x( 'Add New Park', 'mmc_level1' ),
> > 'all_items' => _x( 'Parks', 'mmc_level1' ),
> > 'edit_item' => _x( 'Edit Park', 'mmc_level1' ),
> > 'new_item' => _x( 'New Park', 'mmc_level1' ),
> > 'view_item' => _x( 'View Park', 'mmc_level1' ),
> > 'search_items' => _x( 'Search Parks', 'mmc_level1' ),
> > 'not_found' => _x( 'No Parks found', 'mmc_level1' ),
> > 'not_found_in_trash' => _x( 'No Parks found in Trash', 'mmc_level1' ),
> > 'parent_item_colon' => _x( 'Parent Park', 'mmc_level1' ),
> > 'menu_name' => _x( 'Parks', 'mmc_level1' ),
> > );
> >
> > $args = array(
> > 'labels' => $labels,
> > 'hierarchical' => true,
> > 'description' => '',
> > 'supports' => array( 'title', 'editor', 'author', 'revisions',
> > 'custom-fields', 'comments', 'thumbnail' ),
> > 'public' => true,
> > 'show_ui' => true,
> > 'show_in_menu' => 'mmc_menu',
> > 'show_in_nav_menus' => true,
> > 'publicly_queryable' => true,
> > 'exclude_from_search' => false,
> > 'has_archive' => false,
> > 'query_var' => true,
> > 'can_export' => true,
> > 'rewrite' => array( 'slug' => 'parks', 'with_front' => false ),
> > 'capability_type' => 'page'
> > );
> >
> > register_post_type( 'mmc_level1', $args );
> >
> > $labels = array(
> > 'name' => _x( 'Features', 'mmc_level2' ),
> > 'singular_name' => _x( 'Feature', 'mmc_level2' ),
> > 'add_new' => _x( 'Add New', 'mmc_level2' ),
> > 'add_new_item' => _x( 'Add New Feature', 'mmc_level2' ),
> > 'all_items' => _x( 'eatures', 'mmc_level2' ),
> > 'edit_item' => _x( 'Edit Feature', 'mmc_level2' ),
> > 'new_item' => _x( 'New Feature', 'mmc_level2' ),
> > 'view_item' => _x( 'View Feature', 'mmc_level2' ),
> > 'search_items' => _x( 'Search Features', 'mmc_level2' ),
> > 'not_found' => _x( 'No Features found', 'mmc_level2' ),
> > 'not_found_in_trash' => _x( 'No Features found in Trash', 'mmc_level2' ),
> > 'parent_item_colon' => _x( 'Parent Feature', 'mmc_level2' ),
> > 'menu_name' => _x( 'Features', 'mmc_level2' ),
> > );
> >
> > $args = array(
> > 'labels' => $labels,
> > 'hierarchical' => true,
> > 'description' => '',
> > 'supports' => array( 'title', 'editor', 'author', 'revisions',
> > 'custom-fields', 'comments', 'thumbnail' ),
> > 'public' => true,
> > 'show_ui' => true,
> > 'show_in_menu' => 'mmc_menu',
> > 'show_in_nav_menus' => true,
> > 'publicly_queryable' => true,
> > 'exclude_from_search' => false,
> > 'has_archive' => false,
> > 'query_var' => true,
> > 'can_export' => true,
> > 'rewrite' => array( 'slug' => 'features', 'with_front' => false ),
> > 'capability_type' => 'page'
> > );
> >
> > register_post_type( 'mmc_level2', $args );
> >
> > In the above code, I've been playing back and forth with the rewrite
> > argument, so I couldn't say I'm going down the right path with those.
> > Everything else seems standard.
> >
> > If not the URL routing, what exactly does not work?
> >
> >
> > The URL for the child post is
> > http://example.com/features/yosemite/half-dome/, not the desired
> > http://example.com/parks/yosemite/half-dome/.  Both links lead to a 404
> > error.
> > _______________________________________________
> > wp-hackers mailing list
> > wp-hackers at lists.automattic.com
> > http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list