So I followed a guide on another post here to add a taxonomy to the post type URL which works BUT it breaks the url's for the pages.

My permalink structure is %postname% and my code is as follows:

Post type:

 $args = array(
  'public' => true,
  'publicly_queryable' => true,
  'show_ui' => true,
  'query_var' => true,
  'capability_type' => 'post',
  'hierarchical' => false,
  'menu_position' => null,
  'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
  'rewrite' => array(
   'slug' => '%product_cat%',
   'with_front' => false

 register_post_type( 'kh_products' , $args );

Code to modify the slug:

function change_post_link( $post_link, $id = 0 ){
 $post = get_post($id);  
 if ( is_object( $post ) ){
  $terms = wp_get_object_terms( $post->ID, 'kh_product_cat' );
  if ( $terms ) {
   $replaced = $terms[0]->slug;
   return str_replace( '%product_cat%' , $replaced, $post_link );
 return $post_link;  
add_filter( 'post_type_link', 'change_post_link', 1, 3 );

If you remove the slug line from the post type, the post type url stops working (as intended) and the page url works but add the line back and the pages url's stop working.

I have tried changing the url slug to %%product_cat%% and that stops the post type url from working.

Removing the filter doesn't do anything so the issue is just with the slug string.

Anyone have any ideas on how to accomplish this?

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.