Opened 18 years ago
Closed 17 years ago
#7427 closed enhancement (fixed)
get_categories does not allow exernal taxonomy types
| Reported by: |
|
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)
Change History (20)
#3
@
18 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.
#5
@
18 years ago
As ryan commented you should be using get_terms().
get_categories() is only for the link/post category taxonomies.
#6
@
18 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)...
#7
@
18 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.
#8
@
18 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'];
#12
@
18 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);
#13
@
18 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.
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);