So, WordPress doesn’t allow setting two terms (already existing in a taxonomy or being new, no difference) for a post, when editing it in backend (from Edit or Quick Edit) if they differ only by one accent / diacritical mark, randomly rejecting one of them. After several tests, I found that these terms can still be added to the post using a hook/filter – save_post()
or save_post_{$post->post_type}
. However, there’s a problem here too: WordPress will remove randomly one of the two terms the next time the post will be saved/updated if the hook will be disabled, so the only solution I see in this situation is to add the terms in question to the post before every post saving/updating. Given that post savings are not done very often, it is not a big inconvenience.
This is the code (I chose to use the save_post_{$post->post_type}
hook):
add_action( 'save_post_your_post_type_slug', function( $post_id ) {
$taxonomy = 'custom_taxonomy_slug';
// terms to add; I mention that they were added earlier to the custom
// taxonomy and the taxonomy may contain other terms for other posts
$new_terms = array( 'Mânie', 'Manie' );
// get existing taxonomy terms as an array with their IDs and names
$tax_terms = get_terms( array( 'taxonomy' => $taxonomy,
'fields' => 'id=>name', 'hide_empty' => false ) );
if( ! is_null( $tax_terms ) && ! empty( $tax_terms ) ) {
// check if the new terms exist in the custom taxonomy
$common_terms = array_intersect( $tax_terms, $new_terms );
if( ! is_null( $common_terms) && ! empty( $common_terms) ) {
// get IDs of new post terms
$terms_ids = array_keys( $common_terms );
// set new post terms
wp_set_post_terms( $post_id, $terms_ids, $taxonomy );
}
}
} );