0

I'm attempting to add a category of each child post to that post's permalink. The post type is called lessons. I have a taxonomy called Child Categories that contains the terms workbooks and workbook-solutions

I would like to make it so that if a post is a child of another post and contains either of the terms it is written as the following.

lessons/this-is-a-parent-post/workbooks/this-is-a-child-post
lessons/this-is-a-parent-post/workbook-solutions/this-is-a-child-post

This is the code I've written so far, but it hasn't resulted in the links rewriting.

add_action( 'init', 'add_taxonomy_to_child_post_url' );
function add_taxonomy_to_child_post_url() {  
global $post;
if ( has_term( 'workbooks', $post ) && $post->post_parent ) {
            add_rewrite_rule(
                        '^lessons/([^/]*)/([^/]*)/([^/]*)/?$',
                        'index.php?post_type=lessons&child-categories=$matches[1]&name=$matches[3]',
                        'top'
                    );
    }
    return $post_link;
}
6
  • your questions code adds a filter to the post type link that uses the wpd_post_link function, but it's missing from your question. init is not a filter, you can't return strings, did you mix up init with post_type_link?
    – Tom J Nowell
    Jan 5 at 15:10
  • @TomJNowell You are correct. That was a typo on my part. It has been fixed in my post. I tested it on the site and it is still not rewriting. Jan 5 at 15:18
  • Where is the $id parameter in get_post coming from? Make sure where ever that variable is you have it within the scope of this code! That may be why its not working?
    – YOUGLE
    Jan 5 at 15:50
  • @YOUGLE I've added $id = get_the_ID(); This should pull the post ID number in the current post. This does not result in a rewrite. Jan 5 at 16:08
  • Just revised the code so it is more simplified. Jan 5 at 16:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.