get_category_by_path

Retrieve category based on URL containing the category slug.

Description

(WP_Term|array|WP_Error|null) get_category_by_path( (string) $category_path, (constant) $full_match = true, (string) $output = ); 

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.

Returns (WP_Term|array|WP_Error|null)

Type is based on $output value.

Parameters (3)

0. $category_path (string)
URL containing category slugs.
1. $full_match — Optional. (constant) => true
Whether full path should be matched.
2. $output — Optional. (string)
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.

Usage

  1. if ( !function_exists( 'get_category_by_path' ) ) { 
  2. require_once ABSPATH . WPINC . '/category.php'; 
  3.  
  4. // URL containing category slugs. 
  5. $category_path = ''; 
  6.  
  7. // Optional. Whether full path should be matched. 
  8. $full_match = true; 
  9.  
  10. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to 
  11. // a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  12. $output = ''; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_category_by_path($category_path, $full_match, $output); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category.php  
  1. function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) { 
  2. $category_path = rawurlencode( urldecode( $category_path ) ); 
  3. $category_path = str_replace( '%2F', '/', $category_path ); 
  4. $category_path = str_replace( '%20', ' ', $category_path ); 
  5. $category_paths = '/' . trim( $category_path, '/' ); 
  6. $leaf_path = sanitize_title( basename( $category_paths ) ); 
  7. $category_paths = explode( '/', $category_paths ); 
  8. $full_path = ''; 
  9. foreach ( (array) $category_paths as $pathdir ) { 
  10. $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); 
  11. $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) ); 
  12.  
  13. if ( empty( $categories ) ) { 
  14. return; 
  15.  
  16. foreach ( $categories as $category ) { 
  17. $path = '/' . $leaf_path; 
  18. $curcategory = $category; 
  19. while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) { 
  20. $curcategory = get_term( $curcategory->parent, 'category' ); 
  21. if ( is_wp_error( $curcategory ) ) { 
  22. return $curcategory; 
  23. $path = '/' . $curcategory->slug . $path; 
  24.  
  25. if ( $path == $full_path ) { 
  26. $category = get_term( $category->term_id, 'category', $output ); 
  27. _make_cat_compat( $category ); 
  28. return $category; 
  29.  
  30. // If full matching is not required, return the first cat that matches the leaf. 
  31. if ( ! $full_match ) { 
  32. $category = get_term( reset( $categories )->term_id, 'category', $output ); 
  33. _make_cat_compat( $category ); 
  34. return $category;