Make WordPress Core

Ticket #17587: get_term_by_slug.patch

File get_term_by_slug.patch, 1.1 KB (added by karevn, 14 years ago)
  • wp-includes/taxonomy.php

     
    946946}
    947947
    948948/**
     949 * Find a term in taxonomy by its hierarchical slug.
     950 *
     951 * If the term does not exist - null will be returned.
     952 * If the term exists - term object will be returned.
     953 *
     954 * @param string $slug term slug
     955 * @param string $taxonomy Taxonomy to search in
     956 * @return object Term Row from database. Will return null if $taxonomy does not exist or $term was not found.
     957 */
     958
     959
     960function get_term_by_slug($slug, $taxonomy, $parent = 0){
     961        list($slug, $remaining) = split('/', $slug, 2);
     962        $terms = get_terms(array($taxonomy), array(
     963                'slug' => $slug, 'hide_empty' => false,
     964                'parent' => $parent
     965        ));
     966        if (!count($terms)){
     967                return null;
     968        }
     969        if ($remaining){
     970                return get_term_by_slug($remaining, $taxonomy, $terms[0]->term_id);
     971        }
     972        return $terms[0];
     973}
     974
     975/**
    949976 * Merge all term children into a single array of their IDs.
    950977 *
    951978 * This recursive function will merge all of the children of $term into the same