Custom Post Type URL Structure Issues with Custom Post Type UI Plugin
-
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]
- You must be logged in to reply to this topic.