Change CPT permalink to use the category

You need to update below line at where you have register a custom post type using register_post_type function.

'rewrite' => array('slug' => 'teachers/%cat%')

To change permalink dynamically of post type you have to add below code in functions.php file :

function change_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) ) {
        $terms = wp_get_object_terms( $post->ID, array('teacher-type','year') );
        if ( $terms ) {
            return str_replace( '%cat%', $terms[0]->slug, $post_link );
        }
    }

    return  apply_filters('wpml_permalink', $post_link );
}

add_filter( 'post_type_link', 'change_link', 1, 3 );

//load the template on the new generated URL otherwise you will get 404's the page

function generated_rewrite_rules() {

   add_rewrite_rule(
       '^teachers/(.*)/(.*)/?$',
       'index.php?post_type=teachers&name=$matches[2]',
       'top'
   );
}
add_action( 'init', 'generated_rewrite_rules' );

After that, you need to flush rewrites permalinks, goto the wp-admin > Settings > permalinks. just update permalink setting using “Save Changes” button.

it’ll return urls like below :

  • website.com/teachers/teacher-type/post1
  • website.com/teachers/year/post1

Thank you!