Actually, for what you are trying to do, you do not need to manually add a new rewrite tag or rule.
If you want to change the permalink structure of a custom post type, then use the rewrite
parameter for register_post_type()
and set the rewrite slug to your preferred permalink structure, like so:
$args = array(
'label' => 'Books',
'public' => true,
'rewrite' => array(
// This means that the permalink URL would be
// https://example.com/books/<ISBN>/<post slug>
'slug' => 'books/%isbn%',
),
// Your other args.
);
register_post_type( 'books', $args );
Then use the post_type_link
hook to replace the %isbn%
in the permalink with the correct value.
So looking at your code, you do not need this part: add_action( 'init', 'wpd_test_cpt' );
, but you do need this: add_filter( 'post_type_link', 'wpd_test_post_type_link', 10, 2 );
.
PS: Don’t forget to flush the rewrite rules after removing the unnecessary part above and changing the CPT’s rewrite slug.