How to replace custom post type slug with custom field value by maintaining a specific URL structure?

Question

I want to replace a custom post type slug with custom field value.

I’ve used the below code for that:

add_rewrite_tag( '%employeetype%', 'our-team/([^/]+)' );
add_filter( 'post_type_link', function( $url, $post ){
    if ( 'developer' === $post->post_type ) {
        $customSlug = get_post_meta( $post->ID, 'employee_type', TRUE ) ? get_post_meta( $post->ID, 'employee_type', TRUE ) : 'developers'; // can be two vales develeopers and staff
        $url = str_replace( '%employeetype%', 'our-team/'.$customSlug, $url );
    }
    return $url;
}, 10, 2 );

I also want to maintain the below pages:
mysite.com/our-team/
mysite.com/our-team/developers – this page gives 404
mysite.com/our-team/staff – this page gives 404
mysite.com/our-team/developers/abcd (achieved with above code) works fine

Can we do anything here to achieve this? Thanks.

0
chithra 3 years 2020-03-30T08:50:50-05:00 0 Answers 79 views 0

Leave an answer

Browse
Browse