2

So I have custom post type called Lessons and I am using something like this for the slug

register_post_type( 'Lessons',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Lessons' ),
                'singular_name' => __( 'Lesson' )
            ),
            'public' => true,
            'has_archive' => false,
            'rewrite' => array('slug' => '%postname%-lessons'),
            'show_in_rest' => true,
            'supports' => array( 'title', 'custom-fields','thumbnail' ),
        )
    );

But it's not working, any idea if this is even possible?

1 Answer 1

0

Rewrite is a global base for all posts under the custom post type. For example, this is the default CPT rewrite structure:

example.com/lesson/%postname%

In your code example, it's implying you are trying to do:

example.com/%postname%-lesson/%postname%

Two issues with this idea. The first issue, assuming this functionality worked, is that your CPT urls would all be inconsistent with each other. Because the rewrite slug is global across all your custom posts, every custom post would have their own uniquely different rewrite rule which would be a nightmare to keep track of. The second issue is more relative to the fact that this idea does not work. The rewrite is literal to '%postname%' meaning your post urls would all try to be accessible at exactly /%postname%-lesson/. The % character will break the url entirely and make the post inaccessible.

If you are wanting to remove the CPT slug from in front of the post name, update 'rewrite' to array('with_front' => false),

Then you could implementing a solution such as https://stackoverflow.com/a/4518634. Add in a check against the current post_type, and append any string you want (such as -lesson). This could allow you to achieve a permalink structure such as:

example.com/%postname%-lesson

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.