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);
}
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
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
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
My issue is resolved now. Thank you!
Thank you Waqar! it works perfectly!