You can set up simple tax/cpt permalinks with the rewrite
argument when you register the post type:
function wpd_product_brand_types() {
register_taxonomy(
'brand',
'product',
array(
'rewrite' => array( 'slug' => 'brand', 'with_front' => false )
)
);
register_post_type(
'product',
array(
'label' => 'Products',
'public' => true,
'supports' => array( 'title' ),
'rewrite' => array( 'slug' => 'brand/%brand%', 'with_front' => false )
)
);
}
add_action( 'init','wpd_product_brand_types' );
Then the post_type_link
filter swaps in the %brand%
slug:
function wpd_product_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'product' ){
$terms = wp_get_object_terms( $post->ID, 'brand' );
if( $terms ){
return str_replace( '%brand%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpd_product_permalinks', 1, 2 );