get_category_by_path

The timeline below displays how wordpress function get_category_by_path has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves a category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool   $full_match    Optional. Whether full path should be matched.
 * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                              correspond to a WP_Term object, an associative array, or a numeric array,
 *                              respectively. Default OBJECT.
 * @return WP_Term|array|WP_Error|null Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (('' !== $pathdir) ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms(array('taxonomy' => 'category', 'get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while (0 !== $curcategory->parent && $curcategory->parent !== $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path === $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 5.5

/**
 * Retrieves a category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool   $full_match    Optional. Whether full path should be matched.
 * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
 *                              correspond to a WP_Term object, an associative array, or a numeric array,
 *                              respectively. Default OBJECT.
 * @return WP_Term|array|WP_Error|null Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (('' !== $pathdir) ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms(array('taxonomy' => 'category', 'get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while (0 != $curcategory->parent && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 5.4

/**
 * Retrieve category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool   $full_match    Optional. Whether full path should be matched.
 * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Term|array|WP_Error|null Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (('' != $pathdir) ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms(array('taxonomy' => 'category', 'get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while (0 != $curcategory->parent && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 5.3

/**
 * Retrieve category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool   $full_match    Optional. Whether full path should be matched.
 * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Term|array|WP_Error|null Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (($pathdir != '') ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms(array('taxonomy' => 'category', 'get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while ($curcategory->parent != 0 && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 4.7

/**
 * Retrieve category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool   $full_match    Optional. Whether full path should be matched.
 * @param string $output        Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
 *                              a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
 * @return WP_Term|array|WP_Error|null Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (($pathdir != '') ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms('category', array('get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while ($curcategory->parent != 0 && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 4.3

/**
 * Retrieve category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool $full_match Optional. Whether full path should be matched.
 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
 * @return object|array|WP_Error|void Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (($pathdir != '') ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms('category', array('get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while ($curcategory->parent != 0 && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
}

WordPress Version: 3.7

/**
 * Retrieve category based on URL containing the category slug.
 *
 * Breaks the $category_path parameter up to get the category slug.
 *
 * Tries to find the child path and will return it. If it doesn't find a
 * match, then it will return the first category matching slug, if $full_match,
 * is set to false. If it does not, then it will return null.
 *
 * It is also possible that it will return a WP_Error object on failure. Check
 * for it when using this function.
 *
 * @since 2.1.0
 *
 * @param string $category_path URL containing category slugs.
 * @param bool $full_match Optional. Whether full path should be matched.
 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
 * @return null|object|array Null on failure. Type is based on $output value.
 */
function get_category_by_path($category_path, $full_match = true, $output = OBJECT)
{
    $category_path = rawurlencode(urldecode($category_path));
    $category_path = str_replace('%2F', '/', $category_path);
    $category_path = str_replace('%20', ' ', $category_path);
    $category_paths = '/' . trim($category_path, '/');
    $leaf_path = sanitize_title(basename($category_paths));
    $category_paths = explode('/', $category_paths);
    $full_path = '';
    foreach ((array) $category_paths as $pathdir) {
        $full_path .= (($pathdir != '') ? '/' : '') . sanitize_title($pathdir);
    }
    $categories = get_terms('category', array('get' => 'all', 'slug' => $leaf_path));
    if (empty($categories)) {
        return null;
    }
    foreach ($categories as $category) {
        $path = '/' . $leaf_path;
        $curcategory = $category;
        while ($curcategory->parent != 0 && $curcategory->parent != $curcategory->term_id) {
            $curcategory = get_term($curcategory->parent, 'category');
            if (is_wp_error($curcategory)) {
                return $curcategory;
            }
            $path = '/' . $curcategory->slug . $path;
        }
        if ($path == $full_path) {
            $category = get_term($category->term_id, 'category', $output);
            _make_cat_compat($category);
            return $category;
        }
    }
    // If full matching is not required, return the first cat that matches the leaf.
    if (!$full_match) {
        $category = get_term(reset($categories)->term_id, 'category', $output);
        _make_cat_compat($category);
        return $category;
    }
    return null;
}