Buenas,
Yo tengo algo parecido a lo tuyo, pero sin utilizar el nombre de la taxonomía en la url, es decir, yo tengo http://www.prueba.com/posttype/term/termhijo/ pero entiendo que solo tendrías que añadir a la url a mano tu taxonomía.
Utilizo reglas para redireccionar:
// Rewrites rules Noticias
add_action( 'generate_rewrite_rules', 'register_PostType_rewrite_rules' );
function register_PostType_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
PostType_lang_rewrite('proyecto') . '/([^/]+)/?$' => 'index.php?departamento=' . $wp_rewrite->preg_index( 1 ),
PostType_lang_rewrite('proyecto') . '/([^/]+)/([^/]+)/?$' => 'index.php?post_type=proyecto&departamento=' . $wp_rewrite->preg_index( 1 ) . '&proyecto=' . $wp_rewrite->preg_index( 2 ),
PostType_lang_rewrite('proyecto') . '/([^/]+)/([^/]+)/page/(\d{1,})/?$' => 'index.php?post_type=proyecto&departamento=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ),
PostType_lang_rewrite('proyecto') . '/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=proyecto&departamento=' . $wp_rewrite->preg_index( 2 ) . '&proyecto=' . $wp_rewrite->preg_index( 3 ),
PostType_lang_rewrite('proyecto') . '/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=proyecto&departamento=' . $wp_rewrite->preg_index( 3 ) . '&proyecto=' . $wp_rewrite->preg_index( 4 ),
PostType_lang_rewrite('noticia') . '/([^/]+)/?$' => 'index.php?departamento=' . $wp_rewrite->preg_index( 1 ),
PostType_lang_rewrite('noticia') . '/([^/]+)/([^/]+)/?$' => 'index.php?post_type=noticia&departamento=' . $wp_rewrite->preg_index( 1 ) . '¬icia=' . $wp_rewrite->preg_index( 2 ),
PostType_lang_rewrite('noticia') . '/([^/]+)/([^/]+)/page/(\d{1,})/?$' => 'index.php?post_type=noticia&departamento=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 3 ),
PostType_lang_rewrite('noticia') . '/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=noticia&departamento=' . $wp_rewrite->preg_index( 2 ) . '¬icia=' . $wp_rewrite->preg_index( 3 ),
PostType_lang_rewrite('noticia') . '/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=noticia&departamento=' . $wp_rewrite->preg_index( 3 ) . '¬icia=' . $wp_rewrite->preg_index( 4 ),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
// A hacky way of adding support for flexible custom permalinks
// There is one case in which the rewrite rules from register_kb_rewrite_rules() fail:
// When you visit the archive page for a child section(for example: http://example.com/noticia/category/child-category)
// The deal is that in this situation, the URL is parsed as a Knowledgebase post with slug "child-category" from the "category" section
function fix_PostType_subcategory_query($query) {
$postTypes = array( 'proyecto', 'noticia' );
foreach ( $postTypes as $postType ) {
if ( isset( $query['post_type'] ) && $postType == $query['post_type'] ) {
if ( isset( $query[$postType] ) && $query[$postType] && isset( $query['departamento'] ) && $query['departamento'] ) {
$query_old = $query;
// Check if this is a paginated result(like search results)
if ( 'page' == $query['departamento'] ) {
$query['paged'] = $query['name'];
unset( $query['departamento'], $query['name'], $query[$postType] );
}
// Make it easier on the DB
//$query['fields'] = 'ids';
$query['posts_per_page'] = 1;
// See if we have results or not
$_query = new WP_Query( $query );
if ( ! $_query->posts ) {
$query = array( 'departamento' => $query[$postType] );
if ( isset( $query_old['departamento'] ) && 'page' == $query_old['departamento'] ) {
$query['paged'] = $query_old['name'];
}
}
}
}
}
return $query;
}
add_filter( 'request', 'fix_PostType_subcategory_query', 10 );
function PostType_article_permalink( $article_id, $section_id = false, $leavename = false, $only_permalink = false ) {
$taxonomy = 'departamento';
$article = get_post( $article_id );
$return = '<a href="';
$permalink = ( $section_id ) ? trailingslashit( get_term_link( intval( $section_id ), 'departamento' ) ) : home_url( '/' . PostType_lang_rewrite(get_post_type()) . '/' );
$permalink .= trailingslashit( ( $leavename ? "%$article->post_type%" : $article->post_name ) );
$return .= $permalink . '/" >' . get_the_title( $article->ID ) . '</a>';
return ( $only_permalink ) ? $permalink : $return;
}
function filter_PostType_post_link( $permalink, $post, $leavename ) {
if ( get_post_type( $post->ID ) == 'proyecto' || get_post_type( $post->ID ) == 'noticia' ) {
$terms = wp_get_post_terms( $post->ID, 'departamento' );
$term = ( $terms ) ? $terms[0]->term_id : false;
$permalink = PostType_article_permalink( $post->ID, $term, $leavename, true );
}
return $permalink;
}
add_filter( 'post_type_link', 'filter_PostType_post_link', 100, 3 );
function filter_PostType_section_terms_link( $termlink, $term, $taxonomy = false ) {
if ( $taxonomy == 'departamento' ) {
$section_ancestors = get_ancestors( $term->term_id, $taxonomy );
krsort( $section_ancestors );
$termlink = home_url( '/' . PostType_lang_rewrite(get_post_type()) . '/' );
foreach ( $section_ancestors as $ancestor ) {
$section_ancestor = get_term( $ancestor, $taxonomy );
$termlink .= $section_ancestor->slug . '/';
}
$termlink .= trailingslashit( $term->slug );
}
return $termlink;
}
add_filter( 'term_link', 'filter_PostType_section_terms_link', 100, 3 );
La funcion PostType_lang_rewrite() la utilizo para traducir proyecto/project y noticia/new porque es un multisite, pero tu no tendrías que utilizar.
El único pequeño problema que tengo, es que aunque la url tenga prueba.com/proyecto/categoria1/categoria1.1 me dice que el post type es noticia, ya que no sabe con que post type lo coge, y para solucionarlo he echo un pequeño parche, compruebo en la url si tiene noticia o proyecto.
No se si con eso te he dado alguna luz
Saludos