Opened 5 years ago
Closed 5 years ago
#7427 closed enhancement (fixed)
get_categories does not allow exernal taxonomy types
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| 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)
Change History (20)
comment:3
jacobsantos — 5 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.
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)...
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
comment:10
Malaiac — 5 years ago
- Severity changed from minor to trivial
comment:11
Malaiac — 5 years ago
- Severity changed from trivial to normal
comment:12
Malaiac — 5 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
DD32 — 5 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.
comment:14
jacobsantos — 5 years ago
You break the link_category taxonomy with that patch. Looks good.
comment:15
jacobsantos — 5 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.
comment:16
Malaiac — 5 years ago
- Keywords categories, has-patch added; categories removed
comment:17
azaozz — 5 years ago
- Resolution set to fixed
- Status changed from new to closed

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