Title: get_category_by_path
Published: April 25, 2014
Last modified: April 28, 2025

---

# get_category_by_path( string $category_path, bool $full_match = true, string $output = OBJECT ): 󠀁[WP_Term](https://developer.wordpress.org/reference/classes/wp_term/)󠁿|array|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿|null

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#wp--skip-link--target)

Retrieves a category based on URL containing the category slug.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#description)󠁿

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](https://developer.wordpress.org/reference/classes/wp_error/)
object on failure. Check for it when using this function.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#parameters)󠁿

 `$category_path`stringrequired

URL containing category slugs.

`$full_match`booloptional

Whether full path should be matched.

Default:`true`

`$output`stringoptional

The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
a [WP_Term](https://developer.wordpress.org/reference/classes/wp_term/) object, 
an associative array, or a numeric array, respectively.

Default:`OBJECT`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#return)󠁿

 [WP_Term](https://developer.wordpress.org/reference/classes/wp_term/)|array|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
|null Type is based on $output value.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#source)󠁿

    ```php
    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;
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/category.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/category.php#L125)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/category.php#L125-L179)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#related)󠁿

| Uses | Description | 
| [sanitize_title()](https://developer.wordpress.org/reference/functions/sanitize_title/)`wp-includes/formatting.php` |

Sanitizes a string into a slug, which can be used in URLs or HTML attributes.

  | 
| [_make_cat_compat()](https://developer.wordpress.org/reference/functions/_make_cat_compat/)`wp-includes/category.php` |

Updates category structure to old pre-2.3 from new taxonomy structure.

  | 
| [get_terms()](https://developer.wordpress.org/reference/functions/get_terms/)`wp-includes/taxonomy.php` |

Retrieves the terms in a given taxonomy or list of taxonomies.

  | 
| [get_term()](https://developer.wordpress.org/reference/functions/get_term/)`wp-includes/taxonomy.php` |

Gets all term data from database by term ID.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#)

| Used by | Description | 
| [redirect_canonical()](https://developer.wordpress.org/reference/functions/redirect_canonical/)`wp-includes/canonical.php` |

Redirects incoming links to the proper URL based on the site url.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/get_category_by_path/?output_format=md#comment-content-1378)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_category_by_path/#comment-1378)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_category_by_path%2F%23comment-1378)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_category_by_path%2F%23comment-1378)
 4. **Basic Example**
 5.     ```php
        $categ = get_category_by_path( 'uncategorized' );
        printf( __( 'Category %s', 'textdomain' ), $categ->name );
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_category_by_path%2F%3Freplytocom%3D1378%23feedback-editor-1378)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_category_by_path%2F)
before being able to contribute a note or feedback.