0

I have a custom post type named episodes_listing and a custom taxonomy named episodes_category.

My goal was to have pages like domain.com/episodes/season-one. This works great, and if I have a template called taxonomy-episodes_category.php and someone goes to domain.com/episodes/season-one, using the standard WordPress loop it works fine and shows all of the episodes from season one.

However, when I try to add pagination to this, When someone goes to page 2, the url changes to domain.com/episodes/season-one/page/2 and returns 404. I'm not sure how to get pagination to work.

Pagination code after standard loop endwhile:

next_posts_link( 'Older posts' );
previous_posts_link( 'Newer posts' );

My custom post type/taxonomy code is below.

// Register Custom Post Type
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 Episode', '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/%episodes_category%',
'with_front'          => false
);
$args = array(
'label'               => __( 'episodes', 'text_domain' ),
'description'         => __( 'All  episodes', 'text_domain' ),
'labels'              => $labels,
'supports'            => array('title' ),
'taxonomies'          => array('episodes_category' , 'post_tag'),
'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'         => 'episodes',
'exclude_from_search' => false,
'publicly_queryable'  => true,
'rewrite'             => $rewrite,
'capability_type'     => 'post',
'query_var'           => true,
    'menu_icon' => 'dashicons-media-video'
);
register_post_type( 'episodes_listing', $args );

}


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 Category', '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 );

}

1 Answer 1

4

Are you sure that you don't have a page with the same name of the custom post type? If you do, the permalink rewrite rules will get confused and trigger a 404.

A very simple solution is: The page that lists the custom post types is called in plural (eg. products) and the actual post type name is in singular (eg. product). So they don't conflict and it's all fine.

Also always make sure to rewrite your .htaccess file when creating a new Custom Post Type.

3
  • The custom post type/taxonomy works as it should as far as permalinks. When I create a new category (taxonomy term) under the post type episodes for example season two, it will autoamtically make /episodes/season-two and show on that page show all of the episodes for season-two. Which is the behavior I do want. However, it's been a few months and I know realize I need pagination and that's where I'm getting a little lost. Thanks for any help Bruno.
    – James
    May 25, 2015 at 1:23
  • On further investigation, under permalinks if I change it to "default" instead of post name, the url changes from /episodes/season-one to ?episodes_category=season-two. At this point ?episodes_category=season-two&paged=2 does work. If I change it back to post name episodes/season-one works but not episodes/season-one/page/2 which returns a 404. So pagination seems to be working, it's the URLs that are not. Not sure where to go from here.
    – James
    May 26, 2015 at 14:54
  • my god i finally found the answer..
    – AGrush
    Apr 19, 2022 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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