0

I'm trying to change the permalink structure of the terms for a custom taxonomy, but it's returning page 404. I want the URL to be: example.com/products/category/category-name.

I tried to use the term_link filter, I even tried to put a slug different from the custom post type, however, without success. I have also updated wp_options on permanent links

add_action( 'init', 'register_sps_products_post_type' ); function register_sps_products_post_type() { register_post_type( 'sps-product', array( 'labels' => array( 'name' => 'Products', 'menu_name' => 'Product Manager', 'singular_name' => 'Product', 'all_items' => 'All Products' ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'post-formats', 'revisions' ), 'hierarchical' => false, 'has_archive' => 'products', 'taxonomies' => array('product-category'), 'rewrite' => array( 'slug' => 'products' ) ) ); register_taxonomy( 'product-category', array( 'sps-product' ), array( 'labels' => array( 'name' => 'Product Categories', 'menu_name' => 'Product Categories', 'singular_name' => 'Product Category', 'all_items' => 'All Categories' ), 'public' => true, 'hierarchical' => true, 'show_ui' => true, 'rewrite' => array( 'slug' => '%sps-product%/category', 'with_front' => false ), ) ); } add_filter('term_link', 'idinheiro_permalink_archive_cpt', 10, 2); function idinheiro_permalink_archive_cpt( $url ) { if ( false !== strpos( $url, '%sps-product%') ) { $url = str_replace( '%sps-product%', 'products', $url ); } return $url; } 

    1 Answer 1

    0

    If you just want the category permalink be in the form of: example.com/products/category/<category slug>, then you don't need to hook on term_link.

    Instead, you could just set the taxonomy's rewrite slug to products/categoryand you would also register the taxonomy first then followed by the post type. That way, WordPress would see the request (at the above permalink/URL) as a (single) term request and not a post type request.

    So for example:

    function register_sps_products_post_type() { register_post_type( 'sps-product', array( 'labels' => array( /* your code */ ), 'public' => true, // ... your other args. 'rewrite' => array( 'slug' => 'products' ), ) ); } function register_sps_product_category_taxonomy() { register_taxonomy( 'product-category', array( 'sps-product' ), array( 'labels' => array( /* your code */ ), 'public' => true, // ... your other args. 'rewrite' => array( // Use products/category as the rewrite slug. 'slug' => 'products/category', 'with_front' => false, ), ) ); } // Register the taxonomy first. add_action( 'init', 'register_sps_product_category_taxonomy' ); // Then register the post type. add_action( 'init', 'register_sps_products_post_type' ); // No need to hook on term_link, thus I commented out this part: //add_filter('term_link', 'idinheiro_permalink_archive_cpt', 10, 2); 

    And remember to flush the rewrite rules — just visit the permalink settings (admin) page.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.