How to Set Permalink to Fetch All Children Custom Posts with Parent Post in WordPress

Custom post types are a powerful feature of WordPress that allow you to create custom content types. For example, you could create a custom post type for products, services, or events.

If you have a parent-child relationship between your custom post types, you may want to set up a permalink structure that fetches all children custom posts with the parent post. This can be useful for creating archives of related content.

To set up a permalink structure for child custom posts, you will need to use the following code in your theme’s functions.php file:

function custom_post_type_permalink($post_link, $post, $leavename) {
    if ($post->post_type == 'child-post-type') {
        $parent_post = get_post($post->post_parent);
        $post_link = str_replace($parent_post->post_name, $parent_post->post_name . '/%postname%/', $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'custom_post_type_permalink', 10, 3);

This code will add the parent post’s slug to the permalink of all child custom posts. For example, if your parent post has the slug “products” and your child post has the slug “product-1”, the permalink for the child post will be “products/product-1”.

Once you have added this code to your theme, you will need to flush your permalinks. You can do this by going to Settings > Permalinks and clicking the Save Changes button.

After you have flushed your permalinks, the new permalink structure will be applied to all child custom posts.

Related Posts
Learn How to Fix WordPress Search Not Working (3+ Major Issues Resolved)

Are you looking to solve WordPress search issues on your website? Troubleshooting WordPress search issues may be difficult. This is Read more

How to Fix WordPress\’s White Screen of Death issue

Don\'t be alarmed if you get a WordPress error message or a white screen. Someone has most certainly seen the Read more

WordPress Installation

WordPress Installation Procedures Get the package at http://www.wordpress.org. Open your root web server and install WordPress. When you extract it, Read more

How to Customize WordPress Site

Customize Your WordPress Site Navigate to http://localhost/thedemostore/wp - admin. You should get something like this: admin is the user name. Read more

About WordPress Post

What exactly is a WordPress Post? How to Create and Update a WordPress Post? A CMS can have various types Read more

Image Shared on WordPress to Facebook Not Coming Up? Here\’s what to do.

  When you share your blog posts or web pages on Facebook, the picture might not show up the way Read more

What are WordPress wp_head & wp_footer functions

The wp_head and wp_footer methods are two important things to add to a WordPress theme. \"Action hooks\" are used to Read more

How to fix There was an error trying to send your message. Please try again later.

You make contact forms to make it easy for your audience to get in touch with you. Whether you use Read more

Scroll to Top