0

Based on the link maioman posted, below I have revised this question:

I am having trouble getting a custom post type/taxonomy to work. I want a custom post type of Episodes, and the taxonomy for the custom post type to be Episodes with terms that are generated by the user.

I was able to get

episodes/custom-term-name/ to work. If you go here, it shows all of the posts that are tied to this term.

However if you go to a single post at episodes/custom-term-name/post-title it gives me a 404 page.

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')) {

    //echo 'This is the '. $link;
    $link = str_replace('%episode_cat%', array_pop($cats)->slug, $link);
return $link;
}
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

In attempts to solve the above issue, I then added in

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
 function mmp_rewrite_rules($rules) {
  $newRules  = array();
  $newRules['episodes/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?custom_post_type_name=$matches[3]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['episodes/(.+)/?$']                = 'index.php?taxonomy_name=$matches[1]'; 

return array_merge($newRules, $rules);
}

But now /episodes/custom-term-name brings me to homepage /episodes/custom-term-name/post-title brings me to homepage

So, nothing works.

I've tried changing the $matches array number.

Note: in my WP reading settings, I have it set to a static page. This static page is what is showing when I call the mmp_rewrite_rules function.

1

You can't do this, at least not in any non-hacky way. You basically want "episodes" to represent 2 different things at the same time, but WP has no way of knowing which is intended for any given path.

I guess you could put some logic at the top of your single post template for your CPT, to check if the "post name" is season-one, season-two etc, and if so handle it accordingly. But it's messy.

0

The answer to this post is here Custom Taxonomy specific to a Custom Post type

I needed change

'has_archive'=> 'true'

to

'has_archive' => 'episodes'

and make the capability_type to post instead of page.

Thanks everyone for the pointers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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