¡Hola!
Tengo una taxonomia que tiene una URL custom llamaada “travel-guide”, luego un Custom Post Type que tiene una URL rewrite como “travel-guide/%travel_guide_tax%”. Y un codigo que luego voy a pegar que reescribe la URL de forma correcta para que termine algo como “midominio.com/travel-guide/argentina/buenos-aires/titulo-del-post-type”.
El problema es que la URL tira 404. Si le reescribo manualmente la URL y le pongo “sin-categoria” la pagina carga correctamente. Cuando le pongo los terminos en la URL tira 404.
¿Alguna idea por qué? ¿De qué me olvide?
Dejo codigo:
/**
* Post Type: Travel Guides.
*/
$labels = [
"name" => __( "Travel Guides", "hello-elementor-child" ),
"singular_name" => __( "Travel Guide", "hello-elementor-child" ),
];
$args = [
"label" => __( "Travel Guides", "hello-elementor-child" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "/travel-guide/%travel_guide_tax%", "with_front" => true ],
"query_var" => true,
"menu_icon" => "dashicons-welcome-widgets-menus",
"supports" => [ "title", "editor", "thumbnail" ],
];
register_post_type( "travel-guide-cpt", $args );
}
add_action( 'init', 'cptui_register_my_cpts' );
/**
* Taxonomy: Paises y Ciudades.
*/
$labels = [
"name" => __( "Paises y Ciudades", "hello-elementor-child" ),
"singular_name" => __( "Pais y Ciudad", "hello-elementor-child" ),
];
$args = [
"label" => __( "Paises y Ciudades", "hello-elementor-child" ),
"labels" => $labels,
"public" => true,
"publicly_queryable" => true,
"hierarchical" => true,
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => [ 'slug' => 'travel-guide', 'with_front' => true, ],
"show_admin_column" => false,
"show_in_rest" => true,
"rest_base" => "travel_guide_tax",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => false,
];
register_taxonomy( "travel_guide_tax", [ "travel-guide-cpt" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes' );
function my_post_type_link_filter_function($post_link, $post) {
if (strpos('%travel_guide_tax%', $post_link) === FALSE) {
// Get taxonomy terms
$terms = get_the_terms($post->ID, 'travel_guide_tax');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug.'/'.$terms[1]->slug; //build here
else $taxonomy_slug = 'sin-categoria';
return str_replace('%travel_guide_tax%', $taxonomy_slug, $post_link);
}
}
add_filter('post_type_link', 'my_post_type_link_filter_function', 10, 2);
¡Gracias!