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/category
and 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.