Opened 5 years ago

Closed 5 years ago

#7427 closed enhancement (fixed)

get_categories does not allow exernal taxonomy types

Reported by: Malaiac Owned by: anonymous
Priority: high Milestone: 2.7
Component: General Version: 2.7
Severity: normal Keywords: categories, has-patch
Cc:

Description

/*
wp-includes/category.php
*/
function &get_categories($args = '') {
	$defaults = array('type' => 'category');
	$args = wp_parse_args($args, $defaults);

	$taxonomy = 'category';
	if ( 'link' == $args['type'] )
		$taxonomy = 'link_category';
	$categories = get_terms($taxonomy, $args);

	foreach ( array_keys($categories) as $k )
		_make_cat_compat($categories[$k]);

	return $categories;
}

should be :

function &get_categories($args = '') {
	$defaults = array('type' => 'category');
	$args = wp_parse_args($args, $defaults);

	$taxonomy = 'category';
	global $wp_taxonomies;
	if($args['type'] && $wp_taxonomies[$args['type']]) 
		$taxonomy = $args['type'];

	$categories = get_terms($taxonomy, $args);

	foreach ( array_keys($categories) as $k )
		_make_cat_compat($categories[$k]);

	return $categories;
}

Attachments (3)

7427.diff (759 bytes) - added by DD32 5 years ago.
7427.2.diff (803 bytes) - added by DD32 5 years ago.
category.php.diff (518 bytes) - added by Malaiac 5 years ago.
a less invasive way of allowing external taxonomy would be a filter

Download all attachments as: .zip

Change History (20)

context : I'm developping a directory plugin for WordPress, including a new taxonomy type :

$wp_taxonomies['zelist_category'] = (object) array('name' => 'zelist_category', 'object_type' => 'post', 'hierarchical' => true);

  • Milestone set to 2.7

It is my understanding that you would pass the argument

get_categories("type=zelist_category");

for your custom taxonomy object.

get_categories("type=link");

in my understanding is for backwards compatibility with older taxonomies, or to simplify the link_category taxonomy. It should not interfere with what you are trying to do.

comment:4   ryan5 years ago

Use get_terms()

As ryan commented you should be using get_terms().

get_categories() is only for the link/post category taxonomies.

I gave it a try.

Since the items (links to websites) have categories too, if I were to use get_terms, I'd have to duplicate many WP functions using get_categories (like wp_dropdown_categories() for instance).

And I think that any new taxonomy could include categories (and categories functions as well)...

comment:7   ryan5 years ago

None of the category template functions accept other taxonomies. Most people create their own template functions for their new taxonomy since the category functions are too specific to post categories for them. Adding a taxonomy argument to the category templates so that other taxonomies that don't mind using the "Category" nomenclature can make use of them might be doable.

I'll have to release a plugin with a modification to WordPress core, waiting for the modification. (or have to recode hundreds of lines of WP core just for two lines missing)

The actual change could be :

	if ( 'link' == $args['type'] )
		$taxonomy = 'link_category';
+++  elseif($args['type'] != 'category')
+++    $taxonomy = $args['type'];
  • Priority changed from normal to high
  • Type changed from defect to enhancement
  • Severity changed from minor to trivial
  • Severity changed from trivial to normal

Actually , a filter would work :

$taxonomy = 'category';
	if ( 'link' == $args['type'] )
		$taxonomy = 'link_category';
	$categories = get_terms($taxonomy, $args);
+++	$categories = apply_filter('get_categories',$categories,$args);
  • Version set to 2.7

See also: http://comox.textdrive.com/pipermail/wp-hackers/2008-September/021655.html (and furthur posts in that thread)

The main issue here is not that get_categories() is only for categories, But the fact that many template functions rely upon the function, Duplicating get_categories() for a different type of taxonomy is perfectly ok in my mind, however by doing so, would also require any plugins which add custom category taxonomies to duplicate all the builtin WordPress category template functions, Where a simple addition could mean that it could be achieved by passing a simply 'taxonomy' arg down the line.

I'm going to add a small patch that seems like it doesnt step on the toes, yet also allows code-reuse of the template functions.

DD325 years ago

You break the link_category taxonomy with that patch. Looks good.

DD325 years ago

If the patch is committed, the inline documentation for a lot of functions will need to be updated to info the developer that they can pass 'taxonomy' argument from other functions.

Malaiac5 years ago

a less invasive way of allowing external taxonomy would be a filter

  • Keywords categories, has-patch added; categories removed
  • Resolution set to fixed
  • Status changed from new to closed

(In [9080]) Add filter to get_categories(), props Malaiac, fixes #7427

Note: See TracTickets for help on using tickets.