Skip Navigation

[Resolved] I need to show the category after the custom post type in the URL

This support ticket is created 5 years 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 0.08 hours from now. Thank you for your understanding.

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

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

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 5 years ago.

Assisted by: Waqar.

Author
Posts
#1391983

Tell us what you are trying to do?
I'm trying to show the category after the custom post type in the URL.. I have 2019 as a category.. so hidden link .... I want this to be hidden link

Is there any documentation that you are following? No

Is there a similar example that we can see? No

What is the link to your site? hidden link

#1392325

Hi Ryan,

Thank you for contacting us and I'd be happy to assist.

This will require adding custom rewrite rule and then filtering that post type's URL, as explained in this reply:
https://wordpress.stackexchange.com/a/94820

For example, suppose that you have a custom post type with slug "book" and the slug of the custom taxonomy that you'd like to include the term from is "book-type-slug".

In your custom post type's edit screen, you can select "Use a custom URL format" option with the text "book/book-type"
Screenshot: hidden link

And in your theme's "functions.php" file, you can include the custom filter to replace the static "book-type" part with the first attached term's slug:


function wpa_course_post_link( $post_link, $id = 0 ){
	$post = get_post($id);  
	if ( (is_object( $post )) && ( $post->post_type == 'book' ) ){
		$terms = wp_get_object_terms( $post->ID, 'book-type-slug' );

		if ( ! empty( $terms ) ) {
			if ( ! is_wp_error( $terms ) ) {
				return str_replace( 'book-type' , $terms[0]->slug , $post_link );
			}
		}
	}
	return $post_link;  
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar