url rewriting – Trying to add array of paths to post permalink

Question

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 . "https://wordpress.stackexchange.com/";
        }
        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

0
Мохамед Русланович 3 months 2022-05-29T04:37:18-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse