Skip Navigation

[Resolved] Rewrite permalinks for two post types

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user's custom permalink structure was not working for one of his CPT.

The user's code was

add_filter( 'post_type_link', 'my_post_type_link2', 98, 2 );
function my_post_type_link2( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
          
        if($post_type == "projects") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}
 
add_filter( 'post_type_link', 'type_link', 99, 2 );
function type_link( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
          
        if($post_type == "projects1") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

Solution:
By right the user's code should've worked but it doesn't and I recommended that the functions be combined.

add_filter( 'post_type_link', 'my_post_type_link2', 99, 2 );
function my_post_type_link2( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
            
        if($post_type == "projects" || $post_type == "projects1") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

Since the functions are exactly the same. Now just ensure that the $post_type slug is exactly the same as the one you had created in types.

This support ticket is created 6 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by chantellel 6 years, 11 months ago.

Assisted by: Shane.

Author
Posts
#610790

I have a found a way to change so my permalink structure so its
website/post-type/id/postname

I have two post types, But only the first post types permalink will change
Projects 1 but not "projects"

add_filter( 'post_type_link', 'my_post_type_link2', 98, 2 );
function my_post_type_link2( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
         
        if($post_type == "projects") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

add_filter( 'post_type_link', 'type_link', 99, 2 );
function type_link( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
         
        if($post_type == "projects1") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

I have ticked re-write links on the custom post type settings to projects1/%post_id% and projects/%post_id%

Can some one help please

#610855

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hello,

Thank you for contacting our support forum.

I see from your code that the priority of the function that is not working is set to 98.

Could you change this to 99 and let me know if it starts working.

Thanks,
Shane

#610874

changed to

add_filter( 'post_type_link', 'my_post_type_link2', 99, 2 );
function my_post_type_link2( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
          
        if($post_type == "projects") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}
 
add_filter( 'post_type_link', 'type_link', 99, 2 );
function type_link( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
          
        if($post_type == "projects1") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

Projects url is fine (hidden link),

projects1 is like this hidden link ( i need the post id in this one too)

#610875

If I for example remove the projects code from functions.php then the projects1 will work, if I put the projects one back then only projects will work.

Also If I put it so Projects1 function is first then the projects1 url works and the projects url doesnt.

#610882

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Chantelle,

Could you combine them into 1 function like this.

add_filter( 'post_type_link', 'my_post_type_link2', 99, 2 );
function my_post_type_link2( $post_link, $post = null ) {
    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
           
        if($post_type == "projects" || $post_type == "projects1") {
            return str_replace('%post_id%', $post->ID, $post_link);
        } else {
            return str_replace('%post_id%', '', $post_link);
        }
    }
}

This is because they are pretty much doing the same thing and you can just use one function.

Please let me know if this helps.
Thanks,
Shane

#610892

Hi

Thanks

Tried what you said, Only projects is working still. Projects1 still doesnt

thanks