Permalinks for Taxonomy and Custom Post Type

Have the following situation here:

Custom post type: ‘recipes’
Taxonomy for custom post type ‘recipes’: ‘receitas’
One of the taxonomies slug: ‘receitas-whoopie-pie’

The permalink structure MUST be:

example.com/receitas (show all the posts of custom post type ‘recipes’ … it can be a page template if need)
example.com/receitas-whoopie-pie (show all posts of custom post type ‘recipes’ under the taxonomy ‘recipescat’->’receitas-whoopie-pie’)
example.com/receitas-whoopie-pie/post_name (show a singular post under the taxonomy ‘receitas’->’receitas-whoopie-pie’)

I tried many of ways do this through filters, the only one that works, give me an 404 page when accessing the …/receitas-whoopie-pie/post_name.

Actually, I have the following structure:

My functions regardless this issue:
example.com/receitas (show all the posts of custom post type ‘recipes’ – page template)
example.com/receitas/receitas-whoopie-pie (show all posts of custom post type ‘recipes’ under the taxonomy ‘recipescat’->’receitas-whoopie-pie’)
example.com/recipes/post_name (show a singular post under the taxonomy ‘receitas’->’receitas-whoopie-pie’)

add_action( 'init', 'receitas_posttype', 0 );
function receitas_posttype() {

    $labels = array(
        'menu_name'           => 'Receitas',
        'name'                => 'Receitas',
        'singular_name'       => 'Receita',
        'parent_item_colon'   => '',
        'all_items'           => 'Todas as Receitas',
        'view_item'           => 'Ver Receita',
        'add_new_item'        => 'Adicionar Receita',
        'add_new'             => 'Nova Receita',
        'edit_item'           => 'Editar Receita',
        'update_item'         => 'Atualizar Receita',
        'search_items'        => 'Pesquisar Receita',
        'not_found'           => 'Receita(s) não encontrada(s)',
        'not_found_in_trash'  => 'Receita(s) não encontrada(s) no lixo'
    );  

    $args = array(
        'label'               => 'recipes',
        'description'         => 'Receitas',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'author', 'thumbnail' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 100,
        'menu_icon'           => get_bloginfo('stylesheet_directory').'/images/receitas.png',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    );

    register_post_type( 'recipes', $args );

}

add_action( 'init', 'receitascat', 0 );
function receitascat()  {

    $labels = array(
        'name'                       => _x( 'Receitas', 'Taxonomy General Name', 'ickfd' ),
        'singular_name'              => _x( 'Receita', 'Taxonomy Singular Name', 'ickfd' ),
        'menu_name'                  => __( 'Categorias de Receita', 'ickfd' ),
        'all_items'                  => __( 'Todas as categorias', 'ickfd' ),
        'parent_item'                => __( 'Categoria pai', 'ickfd' ),
        'parent_item_colon'          => __( 'Categoria pai:', 'ickfd' ),
        'new_item_name'              => __( 'Nova categoria', 'ickfd' ),
        'add_new_item'               => __( 'Adicionar nova categoria', 'ickfd' ),
        'edit_item'                  => __( 'Editar categoria', 'ickfd' ),
        'update_item'                => __( 'Atualizar categoria', 'ickfd' ),
        'separate_items_with_commas' => __( 'Separe as categorias com vírgula', 'ickfd' ),
        'search_items'               => __( 'Pesquisar categorias', 'ickfd' ),
        'add_or_remove_items'        => __( 'Adicionar ou remover categorias', 'ickfd' ),
        'choose_from_most_used'      => __( 'Escolha as categorias mais usadas', 'ickfd' ),
    );
    $rewrite = array(
        'slug'                       => 'receitas',
        'with_front'                 => true,
        'hierarchical'               => true,
    );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'receitas', 'recipes', $args );

}

The functions that WORKED, but with 404 page in singular page:

add_action( 'init', 'receitas_posttype', 0 );
function receitas_posttype() {

    $labels = array(
        'menu_name'           => 'Receitas',
        'name'                => 'Receitas',
        'singular_name'       => 'Receita',
        'parent_item_colon'   => '',
        'all_items'           => 'Todas as Receitas',
        'view_item'           => 'Ver Receita',
        'add_new_item'        => 'Adicionar Receita',
        'add_new'             => 'Nova Receita',
        'edit_item'           => 'Editar Receita',
        'update_item'         => 'Atualizar Receita',
        'search_items'        => 'Pesquisar Receita',
        'not_found'           => 'Receita(s) não encontrada(s)',
        'not_found_in_trash'  => 'Receita(s) não encontrada(s) no lixo'
    );  

    $args = array(
        'label'               => 'recipes',
        'description'         => 'Receitas',
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'author', 'thumbnail' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 100,
        'menu_icon'           => get_bloginfo('stylesheet_directory').'/images/receitas.png',
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'rewrite'             => true,
        'capability_type'     => 'post',
    );

    register_post_type( 'recipes', $args );

}

// Ativação das categorias (taxonomias) do custom post type
add_action( 'init', 'receitascat', 0 );
function receitascat()  {

    $labels = array(
        'name'                       => _x( 'Receitas', 'Taxonomy General Name', 'ickfd' ),
        'singular_name'              => _x( 'Receita', 'Taxonomy Singular Name', 'ickfd' ),
        'menu_name'                  => __( 'Categorias de Receita', 'ickfd' ),
        'all_items'                  => __( 'Todas as categorias', 'ickfd' ),
        'parent_item'                => __( 'Categoria pai', 'ickfd' ),
        'parent_item_colon'          => __( 'Categoria pai:', 'ickfd' ),
        'new_item_name'              => __( 'Nova categoria', 'ickfd' ),
        'add_new_item'               => __( 'Adicionar nova categoria', 'ickfd' ),
        'edit_item'                  => __( 'Editar categoria', 'ickfd' ),
        'update_item'                => __( 'Atualizar categoria', 'ickfd' ),
        'separate_items_with_commas' => __( 'Separe as categorias com vírgula', 'ickfd' ),
        'search_items'               => __( 'Pesquisar categorias', 'ickfd' ),
        'add_or_remove_items'        => __( 'Adicionar ou remover categorias', 'ickfd' ),
        'choose_from_most_used'      => __( 'Escolha as categorias mais usadas', 'ickfd' ),
    );

    $rewrite = array(
            'slug'                       => '',
            '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'              => false,
        'rewrite'                    => $rewrite,
    );
    register_taxonomy( 'receitas', 'recipes', $args );

}

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'recipes')
        return $link;

    if ($cats = get_the_terms($post->ID, 'receitas'))
        $link = str_replace('recipes', array_pop($cats)->slug, $link);
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

Thanks in advance.
Sorry for my bad english. 🙁

1 Answer
1

Você já usou Custom post type permalinks?

Have you ever used custom post type permalink?

Ex: example.org/post_type/taxonomy_name/term_slug

Leave a Comment