Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#29899 closed enhancement (wontfix)

wp_insert_term should return an object instead of an array

Reported by: andyexeter's profile andyexeter Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.3
Component: Taxonomy Keywords:
Focuses: Cc:

Description

When using wp_insert_term() I believe the return value should be an object like get_term() and get_term_by()

The rationale behind this can be summarized in this code snippet:

$term = get_term_by('name', 'my-term', 'my-taxonomy');

if($term) {
  $term_id = $term->term_id;
} else {
  $term = wp_insert_term('my-term', 'my-taxonomy');
	
  $term_id = $term['term_id'];
}

If wp_insert_term() returned an object that code could be simplified to:

$term = get_term_by('name', 'my-term', 'my-taxonomy');

if(!$term) {
  $term = wp_insert_term('my-term', 'my-taxonomy');
}

$term_id = $term->term_id;

Change History (2)

#1 @pento
11 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
  • Version changed from 4.0 to 2.3

Thanks for the bug report!

I understand where you're coming from, but I don't think this is a viable change. Naturally, we can't change the default behaviour, so the only other option is add an extra $output parameter. Plugins wanting to use this new parameter would still need to maintain backwards compatibility for old versions of WordPress, which would increase their code complexity.

#2 @andyexeter
11 years ago

Thank you for the reply. Not sure if this is the correct place to be posting this but I ended up casting the returned array to an object with (object)wp_insert_term('my-term', 'my-taxonomy') to get the desired result

Note: See TracTickets for help on using tickets.