Custom Taxonomy Page not working - wordpress

I have created custom post type "directory" and registered custom taxonomy "directory-category". But unable to access taxonomy page "taxonomy-directory-category.php". Getting "Error 404 - Not Found" error.
function create_post_type_listing() {
register_post_type('directory',
array(
'labels' => array(
'name' => __( 'Listings'),
'singular_name' => __( 'Listing'),
'add_new' => __('Add New Listing' ),
'add_new_item' => __('Add New Listing'),
'edit' => __( 'Edit Listing' ),
'edit_item' => __( 'Edit Listing' ),
'new_item' => __( 'New listing' ),
'view' => __( 'View Listing' ),
'view_item' => __( 'View Listing' ),
'search_items' => __( 'Search Listings' ),
'not_found' => __( 'No listings found' ),
'not_found_in_trash' => __( 'No listings found in Trash' ),
'featured_image' => __( 'Listing Image' ),
'set_featured_image' => __( 'Set Listing Image' ),
'remove_featured_image' => __( 'Remove Listing Image' ),
'use_featured_image' => __( 'Use Listing Image' )
),
'public' => true,
'menu_position' => 5,
'menu_icon' => plugins_url( 'images/listing-20x20.png', __FILE__ ),
'rewrite' => array(
'slug' => __('directory')
),
'supports' => array( 'title','editor','thumbnail')));
}
add_action( 'init', 'create_listing_taxonomies', 0 );
function create_listing_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => 'Listing Categories',
'singular_name' => 'Listing Category',
'search_items' => 'Listing Categories',
'all_items' => 'All Listing Categories',
'parent_item' => 'Parent Listing Category',
'parent_item_colon' => 'Parent Listing Category:',
'edit_item' => 'Edit Listing Category',
'update_item' => 'Update Listing Category',
'add_new_item' => 'Add New Listing Category',
'new_item_name' => 'New Listing Category',
'menu_name' => 'Listing Category',
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'directory-category' ),
);
register_taxonomy( 'directory-category', array( 'directory' ), $args );
}
function listing_flush_rules() {
//defines the post type so the rules can be flushed.
create_post_type_listing();
//and flush the rules.
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'listing_flush_rules' );
add_action( 'init', 'create_post_type_listing' );
Kindly help me with taxonomy page structure.
Regards,
Shubhajeet Saha

Please add below code into your functions.php or inside your plugin page
add_action('init', 'create_taxonomies', 0);
function create_taxonomies() {
//change start///
$title = 'Your title';
$slug = 'yourslug';
$your_plugin_textdomain = 'textdomain';
//change end///
$labels = array(
'name' => _x($title, $your_plugin_textdomain),
'singular_name' => _x($title, $your_plugin_textdomain),
'search_items' => __('Search Genres'),
'all_items' => __('All ' . $title),
'parent_item' => __('Parent ' . $title),
'parent_item_colon' => __('Parent ' . $title . ':'),
'edit_item' => __('Edit ' . $title),
'update_item' => __('Update ' . $title),
'add_new_item' => __('Add New ' . $title),
'new_item_name' => __('New ' . $title . ' Name'),
'menu_name' => __( ucfirst($title)),
);
$args = array(
'hierarchical' => true,
'labels' => ($labels),
'show_ui' => true,
// 'show_admin_column' => true, // to display taxonomy in post table
'query_var' => true,
'rewrite' => array('slug' => $slug),
);
register_taxonomy($slug, array($catname[$key]), $args);
}

Related

how to achieve this permalink abc.com/CPTName/CustomPostTypeCategorySlug/categoryname

I have created a cpt named blog. Registered taxonomy for blog as blog_category. I have a taxonomy-blog_category.php for displaying all categories of blog. When i click on a category the url i get is:
http://localhost/mysite/blog_category/category-name/
I need the url like this:
http://localhost/mysite/blog/blog_category/category-name/
function post_type_blog() {
$labels = array(
'name' => _x( 'Blog', 'post type general name' ),
'singular_name' => _x( 'Blog', 'post type singular name' ),
'add_new' => _x( 'Add New', 'Blog' ),
'add_new_item' => __( 'Add New Blog' ),
'edit_item' => __( 'Edit Blog' ),
'new_item' => __( 'New Blog' ),
'all_items' => __( 'All Blogs' ),
'view_item' => __( 'View Blog' ),
'search_items' => __( 'Search Blog' ),
'not_found' => __( 'No Blogs found' ),
'not_found_in_trash' => __( 'No Blogs found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Blog'
);
$args = array(
'labels' => $labels,
'description' => 'Add your Blog',
'public' => true,
'menu_position' => 3,
'menu_icon' => 'dashicons-admin-site',
'supports' => array( 'title', 'thumbnail','editor', 'page-attributes','excerpt','comments'),
'has_archive' => true,
);
register_post_type( 'blog', $args );
}
add_action( 'init', 'post_type_blog' );
function register_taxonomy_for_blog()
{
$labels = [
'name' => _x('Blog categories', 'taxonomy general name'),
'singular_name' => _x('Blog category', 'taxonomy singular name'),
'search_items' => __('Search Blog category'),
'all_items' => __('All Blog categories'),
'parent_item' => __('Parent Blog category'),
'parent_item_colon' => __('Parent Blog category:'),
'edit_item' => __('Edit Blog category'),
'update_item' => __('Update Blog category'),
'add_new_item' => __('Add New Blog category'),
'new_item_name' => __('New Blog category Name'),
'menu_name' => __('Blog categories'),
];
$args = [
'hierarchical' => true, // make it hierarchical (like categories)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => ['slug' => 'blog_category'],
];
register_taxonomy('blog_category', ['blog'], $args);
}
add_action('init', 'register_taxonomy_for_blog');

Wordpress CPT multiple categories in the url

I have a CPT Traffi coffice.
Url: www.domain.com/traffic-offices/
When add a new like www.domain.com/traffic-offices/germany/ its ok.
When i add a new post Saksen then it automaticly makes this url www.domain.com/traffic-offices/saksen but i want www.domain.com/traffic-offices/germany/saksen. If i fill that in it automaticly makes www.domain.com/traffic-offices/germany-saksen.
How can i achieve this url www.domain.com/traffic-offices/germany/saksen ?
function trafficoffice_post_type() {
// Labels
$labels = array(
'name' => _x("trafficoffices", "post type general name"),
'singular_name' => _x("trafficoffices", "post type singular name"),
'menu_name' => 'trafficoffices',
'add_new' => _x("Add new", "trafficoffice item"),
'add_new_item' => __("Add new"),
'edit_item' => __("trafficoffice aanpassen"),
'new_item' => __("Add new trafficoffice"),
'view_item' => __("View trafficoffice"),
'search_items' => __("Search Profiles"),
'not_found' => __("No Profiles Found"),
'not_found_in_trash' => __("No Profiles Found in Trash"),
'parent_item_colon' => ''
);
// Register post type
register_post_type('trafficoffice', array(
'labels' => $labels,
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'traffic-offices',
),
'supports' => array('title', 'editor', 'thumbnail')
));
}
add_action('init', 'trafficoffice_post_type', 0);
One way of doing that is using taxonomies.
Register a new taxonomy called "country".
Create a new category "Germany" with slug "germany".
Assign the category "Germany" to the post "Saksen".
Here is the code to register the taxonomy:
$labels = array(
'name' => _x( 'Countries', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Country', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Countries', 'textdomain' ),
'popular_items' => __( 'Popular Countries', 'textdomain' ),
'all_items' => __( 'All Countries', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Country', 'textdomain' ),
'update_item' => __( 'Update Country', 'textdomain' ),
'add_new_item' => __( 'Add New Country', 'textdomain' ),
'new_item_name' => __( 'New Country Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate Countries with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove Countries', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used Countries', 'textdomain' ),
'not_found' => __( 'No Countries found.', 'textdomain' ),
'menu_name' => __( 'Countries', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'country' ),
);
register_taxonomy( 'country', 'trafficoffice', $args );
Make sure the permalink settings of WordPress (Settings -> Permalinks) include the category.
/%category%/%postname%/
Read more about register_taxonomy().

Check is custom post type

I have a custom post type :
// Custom posttype Events
$labels = array(
'name' => _x('Events', 'Post Type General Name'),
'singular_name' => _x('Events', 'Post Type Singular Name'),
'menu_name' => __('Events'),
'parent_item_colon' => __('Events:'),
'all_items' => __('All Items'),
'view_item' => __('View Item'),
'add_new_item' => __('Add New Event'),
'add_new' => __('Add New'),
'edit_item' => __('Edit Item'),
'update_item' => __('Update Item'),
'search_items' => __('Search Item'),
'not_found' => __('Not found'),
'not_found_in_trash' => __('Not found in Trash'),
);
$args = array(
'labels' => $labels,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'comments', 'trackbacks', 'custom-fields',),
'taxonomies' => array('post_tag'),
'hierarchical' => false,
'rewrite' => array('slug' => __('events')),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type('events', $args);
And a taxonomy for custom post type Events:
// Add new "Type" taxonomy to Events
register_taxonomy('type-events', 'event', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Types', 'taxonomy general name', 'my_theme' ),
'singular_name' => _x( 'Types', 'taxonomy singular name', 'my_theme' ),
'search_items' => __( 'Search Type', 'my_theme' ),
'all_items' => __( 'All Types', 'my_theme' ),
'parent_item' => __( 'Parent Type', 'my_theme' ),
'parent_item_colon' => __( 'Parent Type:', 'my_theme' ),
'edit_item' => __( 'Edit Type', 'my_theme' ),
'update_item' => __( 'Update Type', 'my_theme' ),
'add_new_item' => __( 'Add New Type', 'my_theme' ),
'new_item_name' => __( 'New Type', 'my_theme' ),
'menu_name' => __( 'Types', 'my_theme' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'type-events',
'with_front' => false,
'hierarchical' => true
),
));
In Dashboard Admin, i create two taxonomy type event :
Taxonomies
Custom post type and taxonomy use the same template.
In file template, I want check if it is post type or taxonomy.
Currently, I use is_post_type_archive() to check, but the both return true. That is not what I need.
How to check if this is custom post type or taxonomy?
If you want to check if a post is the custom post type events within the loop you can use this:
<?php if ( get_post_type() === 'events' ) {
/* Do Stuff */
} ?>
If this is outside of the loop, you need to pass the post id into get_post_type():
<?php if ( get_post_type( $post_id ) === 'events' ) {
/* Do Stuff */
} ?>
Edit
You can test for multiple custom post types this way:
<?php if ( get_post_type() === 'events' || get_post_type() === 'promos' || get_post_type() === 'courses' ) {
/* Do Stuff */
} ?>
This is little complicated, maybe the best way is to check with is_post_type_archive() and is_tax(), before you load the themplate, but in your case you may try with get_queried_object().
Codex - get_queried_object
The returned object will have properties taxonomy, term_id, and term_taxonomy_id if this is taxonomy.
So maybe you can use something like get_queried_object()->taxonomy to determine if this is taxonomy and get its name and get_queried_object()->query_var for custom_post_type.
if ( isset( get_queried_object()->taxonomy ) {
//do taxonomy work ...
}
if ( property_exists( get_queried_object()->query_var ) {
//post things
}
function custom_post_type() {
$singular="Car";
$plural="cars";
$labels = array(
'name' => _x( $plural, 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( $singular, 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( $plural, 'text_domain' ),
'name_admin_bar' => __( $singular, 'text_domain' ),
'archives' => __( '$singular Archives', 'text_domain' ),
'attributes' => __( $singular.' Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent '.$singular.':', 'text_domain' ),
'all_items' => __( 'All '.$plural, 'text_domain' ),
'add_new_item' => __( 'Add New '.$singular, 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New '.$singular, 'text_domain' ),
'edit_item' => __( 'Edit '.$singular, 'text_domain' ),
'update_item' => __( 'Update '.$singular, 'text_domain' ),
'view_item' => __( 'View '.$singular, 'text_domain' ),
'view_items' => __( 'View '.$singular, 'text_domain' ),
'search_items' => __( 'Search '.$singular, 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into '.$singular, 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this '.$singular, 'text_domain' ),
'items_list' => __( $singular.' list', 'text_domain' ),
'items_list_navigation' => __( $singular.' list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter '.$singular.' list', 'text_domain' ),
);
$supports =array('title','editor','author','thumbnail','excerpt','trackbacks','custom-fields','comments','revisions','page-attributes','post-formats');
$texonomies =array('category', 'post_tag');
$args = array(
'label' => __( $singular, 'text_domain' ),
'description' => __( $singular.' Description', 'text_domain' ),
'labels' => $labels,
'supports' => $supports,
'taxonomies' => $texonomies,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 8,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'menu_icon' => 'dashicons-video-alt'`enter code here`
);
register_post_type( 'post_type', $args );
}
add_action( 'init', 'custom_post_type');

Custom taxonomy slug before post name

I want that specific custom post url will look like:
http://www.foo.bar/{taxonomy-term-slug}/{postname}
Im using this code to create a custom post type and custom taxonomy
function create_post_types(){
$labels = array(
'name' => _x( "name_plural", 'Post Type General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Post Type Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( "name_plural - Parent:', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All name_plural", 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View name_plural', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'add_new' => __( 'New name_singular', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit name_singular', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update name_singular', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search name_singular', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
'not_found_in_trash' => __( 'Not Found In Trash', 'WP_TEXTDOMAIN' ),
);
$args = array(
'label' => __( "name_plural", 'WP_TEXTDOMAIN' ),
'menu_icon' => "icon",
'description' => __( 'Description', 'WP_TEXTDOMAIN' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
'hierarchical' => false,
'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,
'public' => true,
'capability_type' => 'page',
'query_var' => true,
'rewrite' => array('slug' => '%product_cat%','with_front' => false)
);
register_post_type( 'product', $args );
$labels_cat = array(
'name' => _x( "name_plural", 'Taxonomy General Name', 'WP_TEXTDOMAIN' ),
'singular_name' => _x( "name_singular", 'Taxonomy Singular Name', 'WP_TEXTDOMAIN' ),
'menu_name' => __( 'Categories', 'WP_TEXTDOMAIN' ),
'all_items' => __( 'All Categories', 'WP_TEXTDOMAIN' ),
'parent_item' => __( 'Category - Parent', 'WP_TEXTDOMAIN' ),
'parent_item_colon' => __( 'Category - Parent:', 'WP_TEXTDOMAIN' ),
'new_item_name' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'add_new_item' => __( 'New name_singular Category', 'WP_TEXTDOMAIN' ),
'edit_item' => __( 'Edit Category', 'WP_TEXTDOMAIN' ),
'update_item' => __( 'Update Category', 'WP_TEXTDOMAIN' ),
'view_item' => __( 'View Category', 'WP_TEXTDOMAIN' ),
'separate_items_with_commas' => __( 'Saparate Category With Commas', 'WP_TEXTDOMAIN' ),
'add_or_remove_items' => __( 'Add / Remove Category', 'WP_TEXTDOMAIN' ),
'choose_from_most_used' => __( 'Most Used', 'WP_TEXTDOMAIN' ),
'popular_items' => __( 'Populars', 'WP_TEXTDOMAIN' ),
'search_items' => __( 'Search Category', 'WP_TEXTDOMAIN' ),
'not_found' => __( 'Not Found', 'WP_TEXTDOMAIN' ),
);
$args_cat = array(
'labels' => $labels_cat,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => 'product_cat',
'rewrite' => array('slug' => 'product_cat' ),
'_builtin' => false
);
register_taxonomy( 'product_cat', 'product', $args_cat );
}
// Hook into the 'init' action
add_action( 'init', 'create_post_types', 0);
add_filter('post_link', 'product_permalink', 1, 3);
add_filter('post_type_link', 'product_permalink', 1, 3);
function product_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%product_cat%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'product_cat_cat');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'none';
return str_replace('%product_cat_cat%', $taxonomy_slug, $permalink);
}
The link structure that im getting is correct,
But once i try to click the link i get a 404 Page
Any thoughts ?
Please use this code
// post type product
function custom_post_products() {
$labels = array(
'name' => _x( 'Products', 'post type general name' ),
'singular_name' => _x( 'Product', 'post type singular name' ),
'add_new' => _x( 'Add Product','News' ),
'add_new_item' => __( 'Add Product' ),
'edit_item' => __( 'Edit' ),
'new_item' => __( 'New Product' ),
'all_items' => __( 'All products' ),
'view_item' => __( 'View' ),
'search_items' => __( 'Search' ),
'not_found' => __( 'No record found' ),
'not_found_in_trash' => __( 'No record found in the Trash' ),
'menu_name' => 'Products'
);
$args = array(
'labels' => $labels,
'description' => 'Mcarthur products',
'public' => true,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'menu_position' => 15
);
register_post_type( 'products', $args );
}
add_action( 'init', 'custom_post_products' );
add_action( 'init', 'build_taxonomies_products', 0 );
//custom texonomy
function build_taxonomies_products() {
register_taxonomy( 'products-cat', 'products', array( 'hierarchical' => true, 'label' => 'Categories', 'query_var' => true, 'rewrite' => true ) );
}

WordPress Custom Post Type and Taxonomy URLs

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

Resources