Canonicalization + Custom Post Types not working as expected

Think ive solved it and can’t believe what it was!

On my rewrite rule I had:

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]',
        'top'
    );

Adding in and passing the post_name for the custom post type got it to start redirecting.

add_rewrite_rule( 
        'custom-post-type/([0-9]+)/([^/]*)/?$',
        'index.php?post_type=customposttype&p=$matches[1]&customposttype=$matches[2]',
        'top'
    );

Anycase for now its late and it seems to have started working 🙂 Now at least it forces the 301 from the fake post to the real post as was previously put in canonical meta. Don’t know why but thought if the id was provided it would have worked without having to take in the post_name.

Sometimes WP can really have you pulling your hair out!