## How to properly set the rewrite rules for this case?

Question

I’m having an issue wih the rewrite rules. I can’t figure out how to properly build it to get what i want.

I got a taxonomy with a slug called "acceso-mayorista", which mean a place where other business can see our products.

So terms for this taxonomies can be

I also have a custom post type called "productos-mayoristas" which will have the products. The user will assign a category and the final URL should be:

I manager to get the proper URL, but when i enter it gives 404. I think this is due to the rewrite rules that won’t work properly.

These are the codes:

function cptui_register_my_cpts_productos_mayoristas() {

/**
* Post Type: Mayoristas.
*/

$labels = [ "name" => __( "Mayoristas", "hello-elementor-child" ), "singular_name" => __( "Mayorista", "hello-elementor-child" ), ];$args = [
"label" => __( "Mayoristas", "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" => false, "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" => "acceso-mayorista/%acceso_mayorista%", "with_front" => false ], "query_var" => true, "supports" => [ "title", "editor", "thumbnail" ], ]; register_post_type( "productos-mayoristas",$args );
}

Taxonomy

function cptui_register_my_taxes_acceso_mayorista() {

/**
* Taxonomy: Categorias.
*/

$labels = [ "name" => __( "Categorias", "hello-elementor-child" ), "singular_name" => __( "Categoria", "hello-elementor-child" ), ];$args = [
"label" => __( "Categorias", "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' => 'acceso-mayorista', 'with_front' => false, 'hierarchical' => true, ], "show_admin_column" => false, "show_in_rest" => true, "rest_base" => "acceso_mayorista", "rest_controller_class" => "WP_REST_Terms_Controller", "show_in_quick_edit" => false, ]; register_taxonomy( "acceso_mayorista", [ "productos-mayoristas" ],$args );
}

And this is the code that assign the URLs to the slug

function custom_post_link( $post_link,$id = 0 ){
$current_post = get_post();$current_post_type = $current_post->post_type; if ($current_post_type == 'productos-mayoristas') {

$terms = wp_get_object_terms( get_the_ID(), 'acceso_mayorista');$term_array = array();

// If parent == 0, it means it is a parent and adds it to the beginning of the array, else add it to end of array
foreach ($terms as$term) {
if ($term->parent == 0) { array_unshift($term_array, strtolower($term->name)); } else { array_push($term_array, strtolower($term->name)); } }$tax_url = join('/',$term_array); if( count($term_array) > 0 ){
return str_replace( '%'.'acceso_mayorista%' , $tax_url ,$post_link );
}