I would like to get a term, knowing only part of its slug. The way my slugs are set up, make it so the category name appears before a language code, and I sometimes need to get the term just from the category name and language code concatenated together, but if that exact term doesn't exist, I don't get anything from get_term_by.

That would be okay, except, some of my terms are in multiple languages, and when I know the category and the language, say it's apple in french and english, and I need to find that term from just knowing that I am dealing with apple, and I'm in english.

More concrete example:

term name: apple_en_fr

I want to search for apple_en or apple_fr and get that term.

How could one go about getting a fuzzy match, like that?

2 Answers 2


There're several questions you should ask yourself:

#1) Are the "language codes" actually ISO 639-2 compatible?

Stephen Harris and me build a list of all available ISO 639-2 language codes - [available on GitHub on the WeCodeMore-Label account] - that should be used. This is the official list (build from the official ISO source). In case you're using anything else, then there's already no possibility as the codes are unreliable.

You could now just import that as Git-Submodule into your project, fetch it and compare against it.

#2) Are the strings similar?

You could use - for example - a Levenshtein comparison to check if the string is matching in various combinations that include misspelled combinations.

#3) Is it a sane way to do it?

No. Absolutely not. You should reconsider the way you're doing things. Seems like you got a X-Y-Problem.

  • 1
    +1 for Levenshtein. If was possible +2 for linking the german docs for it.
    – gmazzap
    Commented Aug 31, 2013 at 2:21
  • Yes, they are ISO language codes. But that isn't particularly relevant. The levenshtein idea seems like it could work.
    – steve
    Commented Sep 16, 2013 at 19:20

In case anyone stumbles upon this, I needed to have a search that included fuzzy matches from the function get_terms. I was originally using name__like to help refine search results, but ended up getting all of the terms and used similar_text() to compare the search input vs. the term names.

Below is the function I ended up using. Hope it helps someone:


$args = array(
    'taxonomy'      => array( 'product_cat' ), // taxonomy name
    'orderby'       => 'id', 
    'order'         => 'ASC',
    'hide_empty'    => false,
    'fields'        => 'all'
    //'name__like'    => $search_text  //I TOOK THIS PART OUT
$terms = get_terms( $args );

foreach($terms as $term) {
    $item = similar_text($search_text, $term->name, $percentage);
    if($percentage >= 50) :
        echo $term->name . ' - ' . $percentage . '<br />';

You can modify the percentage threshold to yield the results you want. In my case, this worked great for me. It may not work very well for others in certain cases.

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.