This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by Shekhar Bhandari 4 months ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
June 12, 2019 at 12:08 pm #4004065

manuelL-3

Hi,

Oscar Miras helped me the other day whit this ticket: https://wpml.org/forums/topic/rewrite-url-in-cutom-post-type/

Thanks to his help I was able to solve the problem I was having but now I've realized I need further assistance.

So this is a continuation of the previous ticket I've linked.

I need to append one more taxonomy to the url of the CPT.
The new taxonomy is called promotion and I need it to be after the previous one (called student_types) in the url.

It should look ike this: website.com/students-types/promotion/postname

Do you think you can give me a hand with this last need?

Thanks in advance!

June 13, 2019 at 4:11 am #4009305

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Welcome to WPML support. I'd be assisting you further on this issue.

Does this work without WPML properly? Could you please confirm? Are you using some code to achieve the URL format?

Look forward to your reply.

Thanks

June 13, 2019 at 6:33 am #4009629

manuelL-3

Hi Shekhar and thanks for your help.

I'll explain what I need.

I have a CPT called students and a custom taxonomy called student types. I needed to rewrite the urls for the students cpt to be like this: mywebsite.com/taxonomy-student-types/student1

I had a piece of code that worked fine for the main language to do the rewrite but didn't work for the second language and it gave a 404 error.

Your partner Oscar in this ticket: https://wpml.org/forums/topic/rewrite-url-in-cutom-post-type/ gave me the code below which worked perfectly in the main language and all the translations.


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' => 'student_types',
        'hide_empty' => false,
    ) );
    
    $post_type = 'students';
 
    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 == 'students' ) {
        $resource_terms = get_the_terms( $post, 'student_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);

Now I have realized I need to append one more taxonomy to the url of the CPT.
The new taxonomy is called promotion and I need it to be after the previous one in the url.

So now the url for the CPT should be like this: mywebsite.com/taxonomy-student-types/taxonomy-promotion/student1

So I was wondering if you can gave me a modified version of the previous code to do the new url rewrite and to be compatible with the url translations managed by WPML.

Thanks!

June 13, 2019 at 7:09 am #4009785

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

I am sorry but providing custom code solution falls outside the scope of our support.

At this point, I would suggest you consider contacting one of our certified partners from this link: http://wpml.org/documentation/support/wpml-contractors/

Before doing so please have a look also here:
http://wpml.org/documentation/support/wpml-contractors/guidelines-for-people-looking-for-consulting-help/

You will get the custom assistance you need to get on with your project.

Thanks