I've registered a custom post type "book" and a taxonomy "authors" as below:
add_action( 'init', 'post_book_init' );
function post_book_init() {
$labels = array(
'name' => 'BOOKs',
'singular_name' => 'BOOK',
'menu_name' => 'BOOKs',
'name_admin_bar' => 'BOOK',
'add_new' => 'Add New',
'add_new_item' => 'Add New BOOK',
'new_item' => 'New BOOK',
'edit_item' => 'Edit BOOK',
'view_item' => 'View BOOK',
'all_items' => 'All BOOKs',
'search_items' => 'Search BOOKs',
'parent_item_colon' => 'Parent BOOKs',
'not_found' => 'No BOOK found.',
'not_found_in_trash' => 'No BOOK found in Trash.'
);
$args = array(
'labels' => $labels,
'taxonomies' => array('authors'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => false,
'with_front' => false,
'capability_type' => 'post',
'has_archive' => 'bookarchives',
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks','custom-fields','comments','revisions','page-attributes','post-formats' )
);
register_post_type('book', $args );
$labels = array(
'name' => 'Authors',
'singular_name' => 'Author',
'search_items' => 'Search Author',
'all_items' => 'All Authors',
'parent_item' => 'Parent Author',
'parent_item_colon' => 'Parent Author:',
'edit_item' => 'Edit Author',
'update_item' => 'Update Author',
'add_new_item' => 'Add New Author',
'new_item_name' => 'New Author Name',
'menu_name' => 'Author'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'authors' ),
);
register_taxonomy('authors', 'book', $args );
global $wp_rewrite;
$book_structure = '/book/%authors%/%book%-blah-blah-blah';
$wp_rewrite->add_rewrite_tag("%book%", '([^/]+)', "book=");
$wp_rewrite->add_permastruct('book', $book_structure, false);
unset($labels);
unset($args);
}
I want to use taxonomy term in permalink and applied rewrite rules after registration of post & taxonomy. And also replaced taxonomy term using below code:
add_filter('post_type_link', 'author_permalink_structure', 10, 4);
function author_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, '%authors%')) {
$author_type_term = get_the_terms($post->ID, 'authors');
if (!empty($author_type_term))
{
$author_slug = array_pop($author_type_term)->slug;
$parent_slug=get_term(get_term_by('slug',$author_slug,'authors')->parent,'authors')->slug;
$post_link = str_replace('%authors%', $author_slug, $post_link);
}
else
$post_link = str_replace('%authors%', 'uncategorized', $post_link);
}
return $post_link;
}
So the following links works fine: localhost/book/[authors]/[book-title]-blah-blah blah and localhost/book/[authors] But I also need parent taxonomy slug with child taxonomy like [parent-taxonomy-slug]/[child-taxonomy-slug] I tried concating parent & child slugs with "/" but it returns 404 page not found, although concating with "-" works fine.
$author_slug = array_pop($author_type_term)->slug;
$parent_slug=get_term(get_term_by('slug',$author_slug,'authors')->parent,'authors')->slug;
$post_link = str_replace('%authors%', $parent_slug . "-" .$author_slug , $post_link);
How can I use "[parent-taxonomy-slug]/[child-taxonomy-slug]" in permalink?
author
is already a core query var, you should use something else.