0
votes

I made a custom post type and taxonomy so that I could have a structure of /episodes for an archive page that would show all children "terms", /episodes/custom-term to show an archive of a specific term, and /episodes/custom-term/post-title to show single posts.

I was able to get /episodes/custom-term working for any custom term I create. However, I am receiving 404 for /episodes as well as 404 for /episodes/custom-term/post-title.

In the CMS, when I make a post, assuming my custom term is Season One and the post title is Sample Episode. You can go to /episodes/season-one and it will use the "taxonomy-episode_category.php" template to output all posts within Season One. It also knows when creating the post that the permalink is /episodes/season-one/sample-episode however it reaches a 404 page.

I am not sure where to go from here or if I am doing this wrong.

function episodes() {

$labels = array(
'name'                => _x( 'Episodes', 'Post Type General Name', 'text_domain' ),
'singular_name'       => _x( 'Episodes', 'Post Type Singular Name', 'text_domain' ),
'menu_name'           => __( 'Episodes', 'text_domain' ),
'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
'all_items'           => __( 'All Episodes', 'text_domain' ),
'view_item'           => __( 'View Item', 'text_domain' ),
'add_new_item'        => __( 'Add New Item', 'text_domain' ),
'add_new'             => __( 'Add new episode', 'text_domain' ),
'edit_item'           => __( 'Edit Item', 'text_domain' ),
'update_item'         => __( 'Update Item', 'text_domain' ),
'search_items'        => __( 'Search Item', 'text_domain' ),
'not_found'           => __( 'Not found', 'text_domain' ),
'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' )
);

$rewrite = array(
'slug'                => 'episodes/%episode_cat%',
'with_front'          => false,
'pages'               => true,
'feeds'               => true
);
$args = array(
'label'               => __( 'episodes', 'text_domain' ),
'description'         => __( 'All episodes', 'text_domain' ),
'labels'              => $labels,
'supports'            => array('title' ),
'hierarchical'        => true,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'rewrite'             => $rewrite,
'capability_type'     => 'page',
'query_var'           => true,
'_builtin'            => false
);
register_post_type( 'episodes_listing', $args );

}

add_action( 'init', 'episodes', 0 );

function episodes_taxomony() {

 $labels = array(
'name'                       => _x( 'Episodes Categories', 'Taxonomy General Name', 'text_domain' ),
'singular_name'              => _x( 'Episodes', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name'                  => __( 'Episode Categories', 'text_domain' ),
'all_items'                  => __( 'All Items', 'text_domain' ),
'parent_item'                => __( 'Parent Item', 'text_domain' ),
'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
'new_item_name'              => __( 'New Item Name', 'text_domain' ),
'add_new_item'               => __( 'Add new episode', 'text_domain' ),
'edit_item'                  => __( 'Edit Item', 'text_domain' ),
'update_item'                => __( 'Update Item', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'search_items'               => __( 'Search Items', 'text_domain' ),
'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used'      => __( 'Choose from the most used items', 'text_domain' ),
'not_found'                  => __( 'Not Found', 'text_domain' )
);
$rewrite = array(
'slug'                       => 'episodes',
'with_front'                 => false,
'hierarchical'               => true

);
$args = array(
'labels'                     => $labels,
'hierarchical'               => true,
'public'                     => true,
'show_ui'                    => true,
'show_admin_column'          => true,
'show_in_nav_menus'          => true,
'show_tagcloud'              => true,
'query_var'                  => true,
'rewrite'                    => $rewrite
);
register_taxonomy( 'episodes_category', array('episodes_listing'), $args );

}


add_action( 'init', 'episodes_taxomony', 0 );

function filter_post_type_link($link, $post)
{ 
 if ($post->post_type != 'episodes_listing')
  return $link;

 if ($cats = get_the_terms($post->ID, 'episodes_category')) {

  $link = str_replace('%episode_cat%', array_pop($cats)->slug, $link);
  return $link;
 }
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
1
Did you find an answer to this question?dclawson

1 Answers

1
votes

Turns out pagination was working correctly when I set permalink structure to default instead of Post name. To use post name I was able to use the exact code along with a new function:

function add_rewrite_rules() { 
  add_rewrite_rule('episodes/(.+?)/page/?([0-9]{1,})/?$', 'index.php', 'top'); 
} 
add_filter('init', 'add_rewrite_rules')