• Hello,

    I have one custom post type named struttura (accommodation):

    function custom_post_struttura() {
    	$labels = array(
    		'name'               => _x( 'Strutture', 'post type general name' ),
    		'singular_name'      => _x( 'Struttura', 'post type singular name' ),
    		'add_new'            => _x( 'Aggiungi nuova', 'book' ),
    		'add_new_item'       => __( 'Aggiungi nuova Struttura' ),
    		'edit_item'          => __( 'Modifica Struttura' ),
    		'new_item'           => __( 'Nuova Struttura' ),
    		'all_items'          => __( 'Tutte le Strutture' ),
    		'view_item'          => __( 'Vedi Struttura' ),
    		'search_items'       => __( 'Cerca Struttura' ),
    		'not_found'          => __( 'Nessuna Struttura trovata' ),
    		'not_found_in_trash' => __( 'Nessuna Struttura trovata nel cestino' ),
    		'parent_item_colon'  => '',
    		'menu_name'          => 'Strutture'
    	);
    	$args = array(
    		'labels'        => $labels,
    		'description'   => 'Inserisci una nuova struttura',
    		'public'        => true,
    		'menu_position' => 5,
    		'supports'      => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
    		'has_archive'   => true,
    		'hierarchical'	=> true,
    		'rewrite'		=> array('slug' => '%tipologia%/%regione%/%provincia%/%comune%'),
    		'query_var'		=> true,
    	);
    	register_post_type( 'struttura', $args );
    }
    add_action( 'init', 'custom_post_struttura' );

    custom taxonomy named tipologia (type):

    function taxonomies_tipologia() {
    	$labels = array(
    		'name'              => _x( 'Tipologie', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Tipologia', 'taxonomy singular name' ),
    		'search_items'      => __( 'Cerca Tipologia' ),
    		'all_items'         => __( 'Tutte le Tipologie' ),
    		'parent_item'       => __( 'Parent Tipologia' ),
    		'parent_item_colon' => __( 'Parent Tipologia:' ),
    		'edit_item'         => __( 'Modifica Tipologia' ),
    		'update_item'       => __( 'Aggiorna Tipologia' ),
    		'add_new_item'      => __( 'Aggiungi nuova Tipologia' ),
    		'new_item_name'     => __( 'Nuova Tipologia' ),
    		'menu_name'         => __( 'Tipologia' ),
    	);
    	$args = array(
    		'labels' => $labels,
    		'hierarchical' 	=> true,
    		'public'		=> true,
    		'query_var'		=> true,		'rewrite'		=>  array('slug' => 'strutture' ),
    		//'_builtin'		=> false,
    	);
    	register_taxonomy( 'tipologia', 'struttura', $args );
    }
    add_action( 'init', 'taxonomies_tipologia', 0 );

    and custom taxonomy named regione (region):

    function taxonomies_regione() {
    	$labels = array(
    		'name'              => _x( 'Regioni', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Regione', 'taxonomy singular name' ),
    		'search_items'      => __( 'Cerca Regione' ),
    		'all_items'         => __( 'Tutte le Tipologie' ),
    		'parent_item'       => __( 'Parent Regione' ),
    		'parent_item_colon' => __( 'Parent Regione:' ),
    		'edit_item'         => __( 'Modifica Regione' ),
    		'update_item'       => __( 'Aggiorna Regione' ),
    		'add_new_item'      => __( 'Aggiungi nuova Regione' ),
    		'new_item_name'     => __( 'Nuova Regione' ),
    		'menu_name'         => __( 'Regione' ),
    	);
    	$args = array(
    		'labels' => $labels,
    		'hierarchical' 	=> true,
    		'public'		=> true,
    		'query_var'		=> true,
    		'rewrite'		=>  array('slug' => 'strutture', 'hierarchical' => true),
    	);
    	register_taxonomy( 'regione', 'struttura', $args );
    }
    add_action( 'init', 'taxonomies_regione', 0 );

    Now I create two categories on tipologia:

    • hotel
    • villa

    and some categories, subcategories and subsubcategories for regione like these:

    • Tuscany
    • Florence
    • Empoli
    • Vinci

    Where Tuscany is a regione, Florence is a provincia, Empoli is a comune and Vinci is a comune.

    I want to obtain this kind of permalink:

    1. www.example.com/regione
    2. www.example.com/regione/provincia
    3. www.example.com/regione/provincia/comune
    4. www.example.com/tipologia/regione/provincia/comune
    5. www.example.com/tipologia

    I saw this permalink strategy here:

    1. http://www.agriturismo.net/tuscany/
    2. http://www.agriturismo.net/tuscany/florence/
    3. http://www.agriturismo.net/tuscany/florence/empoli/
    4. http://www.agriturismo.net/villa/tuscany/florence/empoli/
    5. http://www.agriturismo.net/villa

    So I have created two filter, one for tipologia:

    add_filter('post_link', 'tipologia_permalink', 1, 3);
    add_filter('post_type_link', 'tipologia_permalink', 1, 3);
    
    function tipologia_permalink($permalink, $post_id, $leavename) {
    
        if (strpos($permalink, '%tipologia%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'tipologia');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            	$taxonomy_slug = $terms[0]->slug;
    
        return str_replace('%tipologia%', $taxonomy_slug, $permalink);
    }

    and one for regione:

    add_filter('post_link', 'regione_permalink', 1, 3);
    add_filter('post_type_link', 'regione_permalink', 1, 3);
    
    function regione_permalink($permalink, $post_id, $leavename) {
    
        if (strpos($permalink, '%regione%/%provincia%/%comune%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'regione');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
            	$taxonomy_slug = $terms[2]->slug.'/';
            	$taxonomy_slug .= $terms[1]->slug.'/';
            	$taxonomy_slug .= $terms[0]->slug;
            }
    
        return str_replace('%regione%/%provincia%/%comune%', $taxonomy_slug, $permalink);
    }

    Now, it works fine but WordPress can’t find the page. This is not an error 404 but not results!

    Can anyone help me?

The topic ‘Custom Post Type with two Custom Taxonomy: permalink problem’ is closed to new replies.