• Resolved davidki

    (@davidki)


    Hi. I created custom post type with pods. It needs to be hierarchical (because of nice breadcrumbs) but I need to have short URL (without hierarchical Permalinks).

    I know that it is possible for custom taxonomy created with pods where I see checkbox “Hierarchical Permalinks” , but there is no such checkbox “Hierarchical Permalinks” for custom post type. Is there any way how to have hierarchical custom post type but without hierarchical Permalinks?

    Thank you,

    David

    • This topic was modified 7 years ago by davidki.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Jim True

    (@jimtrue)

    Howdy David,

    You might be able to get those if you use Custom Post Type Permalinks as it allows more of the construction style that’s provided to Categories and Tags. Nothing built-in will let you do that naturally without some kind of custom coding for the rewrites.

    Let me know if that works out for you,
    Jim

    Thread Starter davidki

    (@davidki)

    Hi Jim and thank you for your reply.

    I tryed Custom Post Type Permalinks plugin, but it doesn’t allowed me to do what I need. I mean to have hierarchical custom post type without hierarchical permalinks.

    I have custom post type “reviews”

    I created these posts in this “reviews” post type:
    – home appliances
    – laundry
    – washing machines
    – AEG Lavamat 85470SL

    Bacause of hierarchical structure, the URL of “AEG Lavamat 85470SL” post is

    https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/

    I need that this URL will look like this:

    https://www.domain.com/reviews/aeg-lavamat-85470sl/

    But there will still be visible hierarchical structure in breadcrumb navigation.

    I spent all Saturday looking for a solution.

    And right now I am able to have this short URL
    https://www.domain.com/reviews/aeg-lavamat-85470sl/

    by using this code in my functions.php file:

    function reviews() {

    add_rewrite_rule(
    ‘^reviews/([^/]+)/?$’,
    ‘index.php?post_type=reviews&name=$matches[1]’,
    ‘top’
    );

    }

    add_action( ‘init’, ‘reviews’, 0 );

    function bvt_reviews_flatten_hierarchies( $post_link, $post ) {
    if ( ‘reviews’ != $post->post_type ) {
    return $post_link;
    }
    $uri = ”;
    foreach ( $post->ancestors as $parent ) {
    $uri = get_post( $parent )->post_name . “/” . $uri;
    }

    return str_replace( $uri, ”, $post_link );
    }
    add_filter( ‘post_type_link’, ‘bvt_reviews_flatten_hierarchies’, 10, 2 );

    But problem with this solution is that both of URLs exists on my websites.

    Short URL:
    https://www.domain.com/reviews/aeg-lavamat-85470sl/

    and also long URL:
    https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/

    And it is not good for SEO to have 2 URLs for one page.

    I need some rewriting rule that automatically redirect from long URL https://www.domain.com/reviews/home-appliances/laundry/washing-machines/aeg-lavamat-85470sl/

    to short URL

    https://www.domain.com/reviews/aeg-lavamat-85470sl/

    I know I can use some redirect plugin and create such redirect by hand. But it need to rewrite automatically because I will have thousands posts.

    I am not programmer and I can not figure it out such rewriting rule by myself.

    If you could help me with such rewriting rule, I would be very gratefull.

    Thank you for your time, Jim.

    David

    • This reply was modified 7 years ago by davidki.
    
    function reviews() {
    
    add_rewrite_rule(
    ‘^reviews/([^/]+)/?$’,
    ‘index.php?post_type=reviews&name=$matches[1]’,
    ‘top’
    );
    
    }
    
    add_action( ‘init’, ‘reviews’, 0 );
    

    This Rule is registered in default.

    If need redirect url, you can use redirect_canonical filter.

    Plugin Contributor Jim True

    (@jimtrue)

    Thanks @toro_unit for helping out!

    @davidki We don’t actually handle rewrite rules for this kind of stuff, because it’s outside the scope of our plugin; we support what is provided by the standard register_post rewrite rules.

    If you need custom rewrites and .htaccess rules, we typically point folks to WordPress StackExchange for getting assistance with that.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hierarchical custom post type without hierarchical permalinks’ is closed to new replies.