Skip Navigation

[Resolved] Adding taxonomy slug to CPT permalink

This support ticket is created 4 years, 6 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 – 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 5 replies, has 2 voices.

Last updated by valentinP-3 4 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1675327
Screenshot 2020-06-24 at 19.33.22.png

I added the below function in functions php, in order to insert the real-estate-category (the slug of the taxonomy) between the Post type slug and the post one. Example: hidden link title
Everything works well in the WP dashboard ( in the back-end we see the correct permalink structure) but we receive 404 error page not found.
I attached as well, the settings in Post Type
Please advice
Thx
Valentin

// custom taxonomy permalinks in Real Estate

add_filter('post_link', 'realestatecategory_permalink', 10, 3);
add_filter('post_type_link', 'realestatecategory_permalink', 10, 3);

function realestatecategory_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%realestatecategory%') === FALSE) return $permalink;

// Get post
$post = get_post($post_id);
if (!$post) return $permalink;

// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'real-estate-category');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'alta catego';

return str_replace('%realestatecategory%', $taxonomy_slug, $permalink);
}

#1675349
#1675979

Hi,

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

From looking into the code you've shared, it seems to be missing the "add_rewrite_rule" part.

On my test website, I was able to make it work using this code:

Post type slug: book
Taxonomy slug: book-type
Custom URL format: book/%booktypecategory%
( screenshot: hidden link )


function change_link( $post_link, $id = 0 ) {
	$post = get_post( $id );
	if( $post->post_type == 'book' ) 
	{
		if ( is_object( $post ) ) {
			# assume that 'book-type' is slug of your taxonomy 
			$terms = wp_get_object_terms( $post->ID, array('book-type') );
			if ( $terms ) {
				return str_replace( '%booktypecategory%', $terms[0]->slug, $post_link );
			}
		}
	}
	return   $post_link ;
}
add_filter( 'post_type_link', 'change_link', 99, 3 );

function generated_rewrite_rules() {
	add_rewrite_rule(
		'^book/(.*)/(.*)/?$',
		'index.php?post_type=book&name=$matches[2]',
		'top'
	);
}
add_action( 'init', 'generated_rewrite_rules' ); 

You're welcome to test this after adjusting for your post type and taxonomy slug and let me know how it goes.

Note: Don't forget to flush rewrite rules after adding these changes, by re-saving the permalink settings ( WP Admin -> Settings -> Permalinks).

regards,
Waqar

#1676231
Screenshot 2020-06-25 at 13.46.36.png
Screenshot 2020-06-25 at 13.46.50.png

Thank you Waqar, the code is working perfectly in english, but not for the translation (Romanian). I attached two printscreens with the translations of taxonomy and post type / wpml
For Romanian version, the function creates the permalink correctly, and even the post preview is working, but not the post after is saved / published (it gives 404)
Please let me know how we can handle also the translation.
Thank you
Valentin

#1677855

Hi Valentin,

Thank you for sharing the update.

I found a code snippet from the WPML forum which seems very relevant:
https://wpml.org/forums/topic/rewrite-url-in-cutom-post-type/#post-3957629

In case this doesn't work, I'll need a clone or snapshot of your website so that I can investigate this in more detail on my own server.
( ref: https://toolset.com/faq/provide-supporters-copy-site/ )

Note: I've set your next reply as private.

regards,
Waqar

#1680189

My issue is resolved now. Thank you!
Thank you Waqar! it works perfectly!