wppaste
WordPress

get_category_by_path( string $category_path, bool $full_match = true, string $output = OBJECT ): WP_Term|array|WP_Error|null

Since
2.1.0
Source
wp-includes/category.php:125
Retrieves a category based on URL containing the category slug.

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 object on failure. Check for it when using this function.

Compatibility

WordPress
since 2.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$category_pathstring
URL containing category slugs.
$full_matchbooloptional
Whether full path should be matched.Default: true
$outputstringoptional
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.Default: OBJECT

Return value

WP_Term|array|WP_Error|null
Type is based on $output value.

Performance profile

How much work a call to get_category_by_path() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_terms().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
44–72

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 119.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_terms()called directly
  • hookthird-party callbacksapply_filters()one call below get_category_by_path()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_category_by_path() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always44–49urldecode(), rawurlencode(), basename(), sanitize_title(), explode(), category()
!empty($categories)61–63urldecode(), rawurlencode(), basename(), sanitize_title(), explode(), category(), reset(), get_term(), _make_cat_compat()
!empty($categories) && !$categories && $path === false67–72urldecode(), rawurlencode(), basename(), sanitize_title(), explode(), category(), get_term(), _make_cat_compat()
!empty($categories) && !$categories && is_wp_error()69–70urldecode(), rawurlencode(), basename(), sanitize_title(), explode(), category(), get_term(), is_wp_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11944–7211
8.511944–7211
8.411944–72119 fewer instructions than PHP 8.3
8.312853–8111
8.212853–8111
8.112853–81111 fewer instruction than PHP 7.4
7.412953–8111

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 5

  • sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
  • get_terms()Retrieves the terms in a given taxonomy or list of taxonomies.
  • get_term()Gets all term data from database by term ID.
  • is_wp_error()Checks whether the given variable is a WordPress Error.
  • _make_cat_compat()Updates category structure to old pre-2.3 from new taxonomy structure.

Used by · 1

Source code

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

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/category.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.