0

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

acceso-mayorista/chairs/traditional

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:

acceso-mayorista/chairs/traditional/custom-post-type-title

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 );
}

add_action( 'init', 'cptui_register_my_cpts_productos_mayoristas' );

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 );
}
add_action( 'init', 'cptui_register_my_taxes_acceso_mayorista' );

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 );
        }

        return $post_link;  
    }
}
add_filter( 'post_type_link', 'custom_post_link', 1, 3 );

And this is the rewrite i had, but i don't think is correct.

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['acceso_mayorista/(.+)/(.+)/?$'] = 'index.php?productos-mayoristas=$matches[3]';
    //$newRules['acceso_mayorista/(.+)/(.+)/?$'] = 'index.php?acceso_mayorista=$matches[2]';

    return array_merge($newRules, $rules);
}

How the rewrite rule should for this case? Thanks!

4
  • The post_type_link filter does not influence rewrite rules, it's just a filter for when the URL is being output on the frontend, e.g. to add a parameter or replace it with a shortlink service version. It modifies HTML, it does not change a posts permalink, and it does not change rewrite rules. Did you resave your permalinks after making the changes? – Tom J Nowell Dec 13 '20 at 0:37
  • Yes yes, i resaved it and the URL is as intended. But when i enter to the post type it shows a 404. I had a rewrite code i found in the internet but i think it's clearly wrong, as i need something related to what i need to achieve. – Carlos Tinca Dec 13 '20 at 2:31
  • hmm if you used that rewrite code you should put it in your question. Currently there are no custom rewrite rules in your question, just rewrite parameters when registering a post type, and a filter to change the value of a href tag on the frontend – Tom J Nowell Dec 13 '20 at 23:33
  • Thanks for taking the time to reply this message Tom. I updated the code with the rewrite code i had, but i don't think it's even close to what i want to achieve. Sadly i don't understand how the rewrite function works – Carlos Tinca Dec 16 '20 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.