0

I have a CPT called teachers and a custom taxonomy called teacher type.

I'm using the code below to rewrite the url for the CPT and instead of being like: website.com/teachers/post1

it changes the slug to use the category: website.com/teacher-type/post1

This is working correctly but now I need to add another taxonomy (year) to the CPT and to the slug and I need it to be like this:

website.com/teacher-type/year/post1

The code I'm using is compatible with WPML.

This is it:

    function resources_cpt_generating_rule($wp_rewrite) {
    global $sitepress, $sitepress_settings;

    $has_filter = remove_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
    $auto_adjust_ids = $sitepress_settings['auto_adjust_ids'];
    $sitepress_settings['auto_adjust_ids'] = 0;

    $rules = array();
    $terms = get_terms( array(
        'taxonomy' => 'teacher_types',
        'hide_empty' => false,
    ) );

    $post_type = 'teachers';

    foreach ($terms as $term) {    

        $rules[ $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&name=$matches[1]';

    }

    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;

    if ( $has_filter ) {
        add_filter( 'terms_clauses', array( $sitepress, 'terms_clauses' ) );
    }
    $sitepress_settings['auto_adjust_ids'] = $auto_adjust_ids;

}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule', 20);

function change_link( $permalink, $post ) {

    if( $post->post_type == 'teachers' ) {
        $resource_terms = get_the_terms( $post, 'teacher_types' );
        $term_slug = '';
        if( ! empty( $resource_terms ) ) {
            foreach ( $resource_terms as $term ) {
                // The featured resource will have another category which is the main one

                $term_slug = $term->slug;
                break;
            }
        }
        $permalink = apply_filters( 'wpml_permalink', trailingslashit( get_home_url() ) . $term_slug . '/' . $post->post_name );
    }
    return $permalink;
}
add_filter('post_type_link',"change_link",10,2);

Any ideas on how to concatenate those two taxonomies in the url?

Thanks for your help!

0

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!

New contributor
Chetan Vaghela is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Thanks for your help vaghela, but I'm afraid that this method doesn't work for me. I need it to have both taxonomies in the url and delete the custom post type name: website.com/teacher-type/year/post1 – xaifu Jun 12 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.