Maybe you are not using the correct hooks nor the correct rewrite rule. You are rewriting to pagename
, which is for pages. You should use name
to get posts by slug.
add_filter('query_vars', 'cyb_add_query_vars');
function cyb_add_query_vars( $vars) {
$vars[] = "item";
return $vars;
}
add_action('init','cyb_add_rewrite_rules');
function cyb_add_rewrite_rules() {
add_rewrite_rule( ''^shows/([^/]+)/([^/]+)/?$'' , 'index.php?post_type=shows&name=$matches[1]&item=$matches[2]' , 'top' );
}
You can also add a endpoint and use URLs like http://my-website.com/wordpress/shows/show-slug/item/episodes
:
add_action('init','cyb_add_endpoint');
function cyb_add_endpoint() {
add_rewrite_endpoint( 'item', EP_PERMALINK );
}
And don’t forget to flush the rewirte rules. If you don’t want to code the flush, visit the permalink settings page in wp-admin area, click on save and the rewrite rules will be flushed.