Support » Plugin: Custom Post Type UI » Custom Post Type URL Structure Issues with Custom Post Type UI Plugin

  • Resolved floriankonrad

    (@floriankonrad)


    Hello,

    I’m using the Custom Post Type UI plugin to create a custom post type called ‘produktseiten’. Each ‘produktseiten’ post is assigned a parent post, and this is reflected in the URL which I have set up with a custom rewrite slug ‘c’.

    The standard URL structure looks like this:

    https://www.example.com/c/parent-post/post-name/

    However, I want to remove the parent slug from the URL while maintaining the hierarchy within the WordPress structure (for breadcrumbs and other purposes). So, the URL should look like this:

    https://www.example.com/c/post-name/

    Here is the code that I’ve been using so far:

    function remove_parent_slug($link, $post) {
        if ('produktseiten' == $post->post_type) {
            $link = home_url(user_trailingslashit("c/" . $post->post_name));
        }
        return $link;
    }
    add_filter('post_type_link', 'remove_parent_slug', 10, 2);
    
    function fix_404_for_custom_post($query) {
        if (!is_admin() && $query->is_main_query() && 2 == count($query->query) && isset($query->query['page'])) {
            $query->set('post_type', array('post', 'produktseiten', 'page'));
        }
    }
    add_action('pre_get_posts', 'fix_404_for_custom_post');
    

    Unfortunately, this results in a 404 error when I try to access the URL https://www.example.com/c/post-name/. It seems like WordPress cannot correctly find the post when the parent slug is missing.

    Can anyone help solve this issue? Is there a setting or filter I can use in conjunction with the Custom Post Type UI plugin to achieve this URL structure?

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Because of the complexities of permalink customization like this, we recommend the folks over at https://wordpress.org/plugins/permalink-manager/ and whatnot instead of trying to do this custom on your own.

    Based on what I’m reading, you’ve changed the post type’s rewrite to be just c and you’re also wanting to not show parent posts in the URL. Meaning the URL has only the post type’s rewrite and then the viewed post’s slug present, regardless of if it’s a child post, or a post without an ancestor. Correct?

    Thread Starter floriankonrad

    (@floriankonrad)

    Thank you for the swift response. Indeed, I have tested a plugin similar to the Permalink Manager. However, there seems to be a specific issue: unfortunately, I can only change the permalinks individually, not collectively, in the editor since it only displays “c/%postname%”. It appears that WordPress autonomously combines the slugs into a single path.

    Yes, you have understood correctly. I’ve changed the post type’s rewrite to be “c” and simultaneously, I don’t want parent posts to be displayed in the URL. This means the URL only contains the post type’s rewrite and the slug of the viewed post, regardless of whether it’s a child post or a post without an ancestor.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Honestly not sure what to suggest and I have to suspect you’d be encountering the issue regardless of using CPTUI or not.

    Closest I can think of would be to remove that c/ portion from your permalinks settings page, and use the rewrite slug section of CPTUI to change the produktseiten to c there. However, that’s not going to handle the removal of the post parent slug. But hopefully at least a step in the right direction?

    Hi @floriankonrad,

    If you would like to get rid off the parents’ slugs from the product permalinks, please simply replace the Permastructure settings from:

    c/%postname%

    to:

    c/%postname_flat%

    Then to apply the new format, please use “Tools -> Regenerate/reset” tool.

    For more details, please check out the below article about (non)hierarchical permalinks:

    https://permalinkmanager.pro/docs/tutorials/wordpress-permalinks-structure/#removing-hierarchy

    Best regards,
    Maciej Bis

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Thanks for chiming in Maciej 😀

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.