0

I face the following situation: after manually adding (in the backend) the terms with names (not slugs) Mânie and Manie, that differs only in an accent/diacritic (they are totally different words with totally different meanings), to a non-hierarchical custom taxonomy (tags), no matter what slugs were set (I tried the slug pairs minie/manie and manie/manie-2 for that term names), when I try to set these terms to a post, also from backend, when I save the post Wordpress randomly accepts only one of them.

To summarize, the terms are (can be) created / added to the taxonomy, but cannot be added together to the post, one of which being randomly excluded while saving the post.

How can I fix this? I haven't found any useful information so far. The non-hierarchical custom taxonomy was added by a code, but I don't think that's the cause of the problem.

6
  • Do you mean the name "Mânie" or the slug Mânie? There are restrictions here that are the result of the way URLs work that directly impact the slug of a term or post. In theory there is no such thing as example.com/category/Mânie, URL's just can't have diacritics or accents, only a restricted subset of ASCII. Any situation otherwise is just your browser trying to be helpful and turning % encoded values into their internationalised versions in the user interface
    – Tom J Nowell
    Commented Feb 10, 2022 at 18:40
  • @TomJNowell I mean terms with names "Mânie" and "Mania", not slugs.
    – Yuri
    Commented Feb 10, 2022 at 18:47
  • 1
    And you're unable to rename a term to Mânie after the fact despite keeping a different slug? Can you share your code?
    – Tom J Nowell
    Commented Feb 10, 2022 at 18:48
  • @TomJNowell 1) I don't use a code for this. Excepting the added non-hierarchical custom taxonomy (tags), I am adding the terms from the standard backend. 2) The terms exists in the custom taxonomy, but WP doesn't accept both for a post, I don't understand why, if they have different slugs.
    – Yuri
    Commented Feb 10, 2022 at 18:54
  • oh :/ this is a programming stack so I'm unsure if this is the place for you to ask this. You might want to ask this on the .org support forums, and you'll want to share the slug and name of all the terms involved
    – Tom J Nowell
    Commented Feb 10, 2022 at 19:09

1 Answer 1

0

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 );
        }
    }
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.