WordPress.org

Ready to get started?Download WordPress

Forums

Slug Clash - Custom Taxonomy (5 posts)

  1. Leo Blanchette
    Member
    Posted 1 year ago #

    taxonomy_1 term = gold
    taxonomy_2 term = gold

    Generates 404 on taxonomy_2 because the slug has been named "gold-2"

    Obviously this should not happen. What would cause this?

  2. Andrew Bartel
    Member
    Posted 1 year ago #

    Slugs are unique unfortunately, even if they have different parents.

  3. Leo Blanchette
    Member
    Posted 1 year ago #

    So maybe in category creation there is a way to force-uniqueness?

  4. Leo Blanchette
    Member
    Posted 1 year ago #

    Understandably this is a hard problem. I think I'll just modify the slug as its being written that way there is no clashes.

    What is the hook for slug-writing in a custom taxonomy?
    Can't seem to find it.

  5. Leo Blanchette
    Member
    Posted 1 year ago #

    This ended being the solution - to avoid my custom taxonomy clashing with my other taxonomy (and the users are very likely to make clashing/like terms) I did this - simply altered the slug of the category term to have an appropriate suffix that would keep unique from other taxonomies.

    function symbiostock_unique_category( $term_id, $tt_id, $taxonomy )
    {
    
        if ( $taxonomy == 'image-type' ) {
    
            if ( isset( $_POST[ 'slug' ] ) && !empty( $_POST[ 'slug' ] ) ) {
                $name = sanitize_title( $_POST[ 'slug' ] ) . '-images';
            } elseif ( isset( $_POST[ 'tag-name' ] ) && !empty( $_POST[ 'tag-name' ] ) ) {
                $name = sanitize_title( $_POST[ 'tag-name' ] ) . '-images';
            } elseif ( isset( $_POST[ 'newimage-type' ] ) && !empty( $_POST[ 'newimage-type' ] ) ) {
                $name = sanitize_title( $_POST[ 'newimage-type' ] ) . '-images';
            }
    
            wp_update_term( $term_id, $taxonomy, array(
    
                 'slug' => $name 
    
            ) );
    
        }
    
    }
    add_action( 'create_term', 'symbiostock_unique_category', 10, 3 );

Topic Closed

This topic has been closed to new replies.

About this Topic