Trying to add array of paths to post permalink

Am trying to add custom path to my article posttype

so my article path will be www.example/parent-post-1/parent-post-2/the-actual-post

i tried to add this code

/**
 * @param $post_link
 * @param $id
 * Добавить название категории продукта к ссылке
 * @return array|mixed|string|string[]
 */
function wpa_course_post_link($post_link, $id = 0)
{
    $post = get_post($id);
    if ($post instanceof WP_Post) {
        $parent = get_field('parents', $post-ID) ?: [];
        $link = '';
        foreach ($parent as $post) {
            $link .= $post-post_name . '/';
        }
        if ($parent) {
            $post_link = str_replace('%parents%/', $link, $post_link);
        }
    }
    return $post_link;
}

add_filter('post_type_link', 'wpa_course_post_link', 1, 3);

its works if the post has only 1 parent post, but if the post has more than 1 parent posts, it shows 404

my CPT UI settings. Custom rewrite slug - articles/%parents%

how to solve this? thank you

Topic rewrite-rules url-rewriting slug Wordpress

Category Web


You shouldn't need to manually handle this with hooks & custom slug settings - just ensure hierarchical is set to true in your register_post_type arguments.

You mention "CPT UI" settings, are you referring to the Custom Post Type UI plugin? If so, make sure Hierarchical is set to True in the Settings metabox.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.