Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#63488 new defect (bug)

Wrong return value for get_category_by_path()

Reported by: milana_cap's profile milana_cap Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: has-patch
Focuses: docs Cc:

Description (last modified by milana_cap)

The function get_category_by_path(), https://developer.wordpress.org/reference/functions/get_category_by_path/ claims to return WP_Term|array|WP_Error|null but it does not return the WP_Error.

Testing:

<?php
$category = get_category_by_path( 'wrongpath' );
if ( ! is_wp_error( $category ) ) { 
   echo 'not an error'; 
}

I've got the string echoed for the non-existent category.

I created a PR with a patch, but @harishtewari has reported it.

Report: https://github.com/WordPress/Documentation-Issue-Tracker/issues/1908
PR: https://github.com/WordPress/wordpress-develop/pull/8844

Change History (5)

This ticket was mentioned in PR #8844 on WordPress/wordpress-develop by @milana_cap.


7 weeks ago
#1

Remove unsuported return type for get_category_by_path.

Trac ticket: https://core.trac.wordpress.org/ticket/63488

#2 @milana_cap
7 weeks ago

  • Description modified (diff)

#3 @sabernhardt
7 weeks ago

[6125] added an is_wp_error() check. If I read the conditions correctly, it could receive an error from get_term() if the $category is a valid subcategory with an invalid parent ID (maybe because the parent term was deleted?).

If $curcategory never produces an error (anymore), the is_wp_error condition could be removed too. Otherwise, the documentation should continue to mention WP_Error.

#4 @opurockey
7 weeks ago

In line 157https://github.com/WordPress/wordpress-develop/blob/666ebd8731818af64f135a28aaffb90881b7ed8c/src/wp-includes/category.php#L157, There might be a slight possibility ( not zero ) that get_term() could return WP_Error in some cases.

Last edited 7 weeks ago by SergeyBiryukov (previous) (diff)

#5 @SergeyBiryukov
7 weeks ago

Hi there, thanks for the ticket.

This is an interesting one. As noted above, get_term() can return a WP_Error if either the term ID is empty or the taxonomy does not exist. So get_category_by_path() can return a WP_Error via get_term(), but it's not easy to trigger that within normal workflow. I think the only case would be when the matching category somehow has an empty term_id value, which is technically possible to reproduce by editing the database or using the get_terms filter:

add_filter( 'get_terms', function( $terms ) {
	foreach ( $terms as $key => $term ) {
		$terms[ $key ]->term_id = 0;
	}

	return $terms;
});

var_dump( get_category_by_path( 'uncategorized' ) );

object(WP_Error)#743 (3) {
  ["errors"]=>
  array(1) {
    ["invalid_term"]=>
    array(1) {
      [0]=>
      string(11) "Empty Term."
    }
  }
  ["error_data"]=>
  array(0) {
  }
  ["additional_data":protected]=>
  array(0) {
  }
}

I also tried unregistering the category taxonomy to see if that would produce a similar outcome, but unregister_taxonomy() has a check for built-in taxonomies, see [36243] / #35227.

Note: See TracTickets for help on using tickets.