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

Mike Schinkel mikeschinkel at newclarity.net
Sat Nov 5 23:54:22 UTC 2011


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



More information about the wp-hackers mailing list