Skip Navigation

[Resolved] Hierarchical url Parent / Child custom post type

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 10 years ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 8:00 – 17:00 -
- - - - - - -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 4 replies, has 3 voices.

Last updated by jonathanB-14 10 years ago.

Assisted by: Adriano.

Author
Posts
#204696

In a reply in this topic https://toolset.com/forums/topic/is-this-possible-hierarchy-question/ is sounds like child post types should have hierarchical urls.

So if I got a Custom Post type called Country and a child post type to this called City when viewing a city post I would get the url siteurl/country/city/postname. But I just get siteurl/city/postname

#204778

Dear bob,

There is no way to do that with the Types plugin alone, but I'm sure you can do it by adding some rewrite rules: http://codex.wordpress.org/Class_Reference/WP_Rewrite

This is a WordPress thing and not something that we control in Types. WordPress permalinks build a hierarchy for pages. Since Types parent / child relationship permits different parents to the same child, there's no way to make the Types parent in the URL.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

#209213

Thanks

#209283

You are welcome.

#376770

I posted this solution on a similar thread elsewhere but just want to get this simple code snippet out there for anyone trying to do this. This solution is assuming a custom URL structure based on a relationship between content types of Brand and Product where I want the URL to be /nutrition/brand-name/product-name/. Hopefully this solution is a help to anyone else looking for a way to do this. If you need further explanation contact me hidden link.

//URL rewrites rules for nutrition/brand/product
add_action( 'init', function() {
    add_rewrite_rule('^nutrition/(.*)/([^/]+)/?$','index.php?product=$matches[2]','top');
});
add_filter('post_type_link', function($link, $post){
    if('product' == get_post_type($post)){
        $parentId = wpcf_pr_post_get_belongs($post->ID,'brand');
        if($parentId){
            $parent = get_post($parentId);
            return str_replace('product', $parent->post_name, $link);
        }

    }
    return $link;
}, 10, 2 );

The forum ‘Types Community Support’ is closed to new topics and replies.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.