• Hola buenos días, les presento mi problema

    Tengo 3 custom post types creados que comparten 1 custom taxonomy que tiene 2 terminos.

    La cuestión es que quiero mostrar un custom post con un termino.

    Es decir, tengo por ejemplo, libros y peliculas como custom post type y como taxonomy tengo genero y como termino tengo accion y aventura. Lo que quiero es filtrar algo que quede como

    misitio.com/micustompost/mitaxonomy/mitermino que seria
    misitio.com/libros/genero/aventura

    En los archivos templates de wordpress existe taxonomy-{mi-taxonomy}-{mi-termino}.php pero no existe nada como taxonomy-{mi-post-type}-{mi-taxonomy}-{mi-termino}.php
    ni tampoco existe algo como archive-{mi-post-type}-{mi-taxonomy}-{mi-termino}.php

    Home

    he visto que varios se han encontrado con esta limitación y lo han podido resolver:
    https://wordpress.org/support/topic/cunstom-taxonomy-archives-with-custom-post-types/?replies=2#post-5089218
    http://codefromabove.com/2016/01/wordpress-custom-taxonomy-archive-for-custom-post-types/

    Pero el tema es que su solución se puede aplicar si tenes un taxonomy con un custom post, en mi caso tengo 3 custom post que comparte una custom taxonomy.

    lo que he visto que es lo que estoy investigando ahora es que el termino se puede filtrar en la url.
    misitio.com/genero/aventura?post_type=libros y toma de template el archive-libros.php

    Alguna ayuda de como resolver este problema? se entiende lo que quiero decir?

Viendo 1 respuesta (de un total de 1)
  • 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 ) . '&noticia=' . $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 ) . '&noticia=' . $wp_rewrite->preg_index( 3 ),
                    PostType_lang_rewrite('noticia') . '/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?post_type=noticia&departamento=' . $wp_rewrite->preg_index( 3 ) . '&noticia=' . $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

Viendo 1 respuesta (de un total de 1)
  • El debate ‘Mostrar custom post type con custom taxonomy term’ está cerrado a nuevas respuestas.