WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

#7427 closed enhancement (fixed)

get_categories does not allow exernal taxonomy types

Reported by: Malaiac Owned by:
Milestone: 2.7 Priority: high
Severity: normal Version: 2.7
Component: General Keywords: categories, has-patch
Focuses: 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 7 years ago.
7427.2.diff (803 bytes) - added by DD32 7 years ago.
category.php.diff (518 bytes) - added by Malaiac 7 years ago.
a less invasive way of allowing external taxonomy would be a filter

Download all attachments as: .zip

Change History (20)

comment:1 @Malaiac7 years ago

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

comment:2 @Malaiac7 years ago

  • Milestone set to 2.7

comment:3 @jacobsantos7 years ago

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 @ryan7 years ago

Use get_terms()

comment:5 @westi7 years ago

As ryan commented you should be using get_terms().

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

comment:6 @Malaiac7 years ago

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 @ryan7 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.

comment:8 @Malaiac7 years ago

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'];

comment:9 @Malaiac7 years ago

  • Priority changed from normal to high
  • Type changed from defect to enhancement

comment:10 @Malaiac7 years ago

  • Severity changed from minor to trivial

comment:11 @Malaiac7 years ago

  • Severity changed from trivial to normal

comment:12 @Malaiac7 years ago

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

comment:13 @DD327 years ago

  • 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.

@DD327 years ago

comment:14 @jacobsantos7 years ago

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

@DD327 years ago

comment:15 @jacobsantos7 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.

@Malaiac7 years ago

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

comment:16 @Malaiac7 years ago

  • Keywords has-patch added

comment:17 @azaozz7 years ago

  • 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.