Find terms in a custom taxonomy based on the hirearchy

You can do this by recursivly checking each level to see if it exists, with the level above as it parent.

While I’ve never used this code in anger, it is tested and working, though I can’t say how efficient it will be, particularly if you have a long hierarchical path.

Place this code in functions.php

/**
 * Return whether or not a given hierarchical taxonomy structure exists
 *
 * @param required array $path  The hierarchical path to check
 * @param string $category      The taxonomy to search
 * @param integer $parent       The parent of the first term
 *       (usually ignored when called by the user, this parameter is primarily
 *        for use when the function is recursivly called)
 * @return boolean              Whether or not the given path exists
 */
function taxonomy_path_exists($path, $taxonomy = 'category', $parent = 0){

    if(!is_array($path) || empty($path))    // Ensure that the '$path' is given as an array, and id not empty
        return false;

    if(term_exists($path[0], $taxonomy)) : // The term exists, recursivly check if the given children exist

        $term = get_term_by('slug', $path[0], $taxonomy);   // Get this term as an object
        unset($path[0]);                                    // Remove this level so that it doesn't get retested
        $path = array_values($path);                        // Reset the keys within the '$path' array

        /**
         * Check if the '$path' array is now empty (meaning that all levels have been exhusted)
         * or if the next level exists
         */
        if(empty($path) || path_exists($path, $taxonomy, $term->term_id)) :
            return ture;
        else :
            return false;
        endif;

    else :  // The term does not exist

        return false;

    endif;

}

To use this function, pass an array of the path to check along with the category it belongs to. I’ve guessed at your term slugs and that you are uisng category, but you can amend as required.

This code will also work for part paths, so if you were to remove 'football' for example, it would still work.

Place this code in your template where you wish to check a given hierarchical path –

$path = array(
    'sports',
    'sports-news',
    'football'
);
echo (taxonomy_path_exists($path, 'category')) ? 'Yes' : 'No';