Skip Navigation

[Resolved] Use Post Id in Permalink of Custom Post Type

This support ticket is created 8 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.

Our next available supporter will start replying to tickets in about 6.34 hours from now. Thank you for your understanding.

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 10 replies, has 2 voices.

Last updated by dbarber 8 years, 11 months ago.

Assisted by: Waqas.

Author
Posts
#272676

I've got a half-dozen custom post types on a site and need to use the post ID in the permalink.

I've searched the forum and found a post which seems to describe the solution simply enough:

URL to thread: https://toolset.com/forums/topic/append-post_id-at-the-end-of-permalink-for-custom-post-type/

Solution presented:

add_filter( 'post_type_link', 'my_post_type_link', 10, 2 );
function my_post_type_link( $post_link, $post = null ) {
if ( !empty($post) ) {
return str_replace('%post_id%', $post->ID, $post_link);
}
}

But no matter what I do (including resaving permalinks) the post ID never shows up.

The slug of the CPT is jobs and I have tried replacing various parts of the above code with the CPT slug, but it's not working for me.

Can someone tell/show me exactly what I need to do (i.e., where in the above code would I add the CPT slug jobs or which parts of it I would replace with the CPT slug jobs)?

Thanks very much.

#272804

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can you please ensure that in permalink settings, you have appended /%post_id% at end of the permalink used? Usually your permalink should be set to Post Name or another base.

Additionally, please try decreasing the priority for the filter below, so it is executed after other default filters and ensures the impact.

add_filter( 'post_type_link', 'my_post_type_link', 99, 2 );
function my_post_type_link( $post_link, $post = null ) {
	if ( !empty($post) ) {
		return str_replace('%post_id%', $post->ID, $post_link);
	}
}

Please notice the 99 in above add_filter line.

Please let me know if I can help you with anything related.

#273024

Hi Waqas

I probably wasn't clear enough in my original message.

I've got six different custom post types but I only want one of the six (called jobs) to show the post id in the permalink. All others should continue to simply show the post name.

Please advise.

#273048

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please find the above code altered around your particular job type, below:

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

Please remember to adjust "jobs" in $post_type == "jobs" to match the exact slug of your said post type.

#273117

Hi Waqas

Thanks for the modified code.

I've added it to functions.php and have resaved permalinks and individual "jobs" posts, but the "jobs" post permalinks still show up without the post ID.

To confirm, "jobs" is the exact slug of the post type.

Please advise.

#273386

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can I ask you for a temporary access to your site? So I can investigate for the issue.

I have enabled your next reply as private, please input all details in that area. Also remember to make functions.php writable for this purpose.

Please remember to take backup of your site before proceeding.

#273749

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for providing the access, please allow me some time to work on this. I will update you as soon as I find a solution.

#273834

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

screenshot-talent-xxxxxx.staging.xxxxxx.com 2015-01-06 17-46-35.png

I have just tried adding post_id to the permalinks of 'jobs' CPT. It looks like there's only 1 possible way and that is to add '%post_id%' before the post name, like below:

<em><u>hidden link</u></em>

However, even to achieve this, I changed a little setting in your CPT as shown in attached image.

And added followings to your theme's functions.php:

add_filter( 'post_type_link', 'my_post_type_link', 99, 2 );
function my_post_type_link( $post_link, $post = null ) {
	$newlink = $post_link;

    if ( !empty($post) ) {
        $post_type = get_post_type($post->ID);
         
        if($post_type == "jobs") {
            $newlink = str_replace('%post_id%', $post->ID, $post_link);
        } else {
            $newlink = str_replace('%post_id%', '', $post_link);
        }
    }
	
	return $newlink;
}

So it works as mentioned above.

I also observed there's something like 'courseware' is in use, which requires permalinks to be set to '%postname%' to work correctly. However, I am not sure if that's the reason, that %post_id% can't be appended in Settings -> Permalinks. Since setting this in there doesn't work but leads to a Bad Request (400).

I will suggest if the above mentioned work around doesn't work for you, you may want to contact with our Certified Partners at https://toolset.com/consultant/. They can work closely with you to help you build your custom solutions.

Please let me know if you would like me to revert all this implementation, in case if you don't want this to retain, thanks.

#273938

Hi Waqas

Thanks for all your help on this.

Adding '%post_id%' before the post name is a great solution in this case.

The only problem I've noticed is that the jobs archive now returns a 404 error.

Would you mind taking a look at this?

Thanks very much.

Dawson

#274058

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Actually when you use a customized logic of URL Rewrite for a CPT, it looses the default handling of WP for archive pages. There are 2 options:

A) You use this custom URL Rewrite logic, create a View to list all jobs with excerpt and link to single job pages, and insert view in a page named "Jobs". So when /jobs is requested, you see an archive.

B) You opt-out for the custom URL logic and standard archive page works.

I will suggest that using option A, creation of Jobs View and Page isn't a big deal. You can keep your current custom logic of %post_id% in place, as well as, can mimic the archive page.

Please notice I have also changed the custom URL logic a little bit for Jobs CPT, as "/jobs/%post_id%". Just to make it more understandable if you utilize the option A.

#274142

Thanks, Waqas.

Mimicking the archive page is a great solution.

I appreciate all your help (and your creativity).

Many thanks,

Dawson

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.