I'm trying to create a custom permalink structure that will allow me to accomplish the following.
- I have a custom post type called "products"
- I have a custom taxonomy called "catalogs" that is assigned to the CPT "products"
I want my permalink structure to look like this:
catalogs/product-name - without CPT slug or
%custom-taxonomy%/%post-name%/
Also need to work archives page(/products).
So far I have managed to achieve such a structure(/%post-name%):
function remove_cpt_slug( $post_link, $post ) {
if ( 'products' === $post->post_type && 'publish' === $post->post_status ) {
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function parse_request ( $query ) {
if ( ! $query->is_main_query() )
return;
if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', array( 'post', 'page', 'products' ) );
}
}
add_action( 'pre_get_posts', 'parse_request' );
Thanks