0

I have a hierarchical custom taxonomy were individual term names could be repeated in different level but not in a single hierarchical path.

Let me explain this using an example

News
News > Regional
News > International
News > International > country
Sports
Sports > News
Sports > News > Football

In the above example the term "News" is repeated twice, but their hierarchical path are different.

Given this custom taxonomy structure and terms I need a way to find if a given hierarchical path is present and if yes, then get the child term id.

Eg: My input for the search would be Sports > News > Football and I need to find the whether such a path exists and if yes then the id of the leaf term (Football in the above case)

Is there a way to do it using built-in WordPress functions?

2
  • Use get_term_children to test if any given term has children – Pieter Goosen Feb 16 '15 at 11:53
  • @PieterGoosen get_term_children can only tell me if a particular term has children, but in my case I need to search for the complete path. I am not sure if I completely understand your suggestion. – Sudar Feb 16 '15 at 12:08
1

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';
5
  • Thanks for the answer. The one problem I have with this code is that the path contains slugs, but in my case it contains names. Since names can have duplicates get_term_by will not work properly :( – Sudar Feb 16 '15 at 15:09
  • Can you not change that so that you use slugs? Exaclty how are you outputting your path? If you can update your question with that info I can see if there is a way to update it, or this answer, so that it works. – David Gard Feb 16 '15 at 15:23
  • Unfortunately I can't change to use slugs. I am trying to see if I can adapt your solution to use names instead of slugs. Meanwhile if you have any solution for it then that would be very useful. Thanks. – Sudar Feb 17 '15 at 3:01
  • Can you post your code? As you have seen with this answer, it's hard to give anything that's accurate without your code. – David Gard Feb 17 '15 at 8:04
  • I used your code as a base and replaced get_term_by with get_terms with parent parameter and little bit of tweak and was finally able to do it. Thanks. – Sudar Feb 20 '15 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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