3 function get_all_category_ids() {
4 if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
5 $cat_ids = get_terms('category', 'fields=ids&get=all');
6 wp_cache_add('all_category_ids', $cat_ids, 'category');
12 function &get_categories($args = '') {
13 $defaults = array('type' => 'category');
14 $args = wp_parse_args($args, $defaults);
16 $taxonomy = 'category';
17 if ( 'link' == $args['type'] )
18 $taxonomy = 'link_category';
19 $categories = get_terms($taxonomy, $args);
21 foreach ( array_keys($categories) as $k )
22 _make_cat_compat($categories[$k]);
27 // Retrieves category data given a category ID or category object.
28 // Handles category caching.
29 function &get_category($category, $output = OBJECT, $filter = 'raw') {
30 $category = get_term($category, 'category', $output, $filter);
31 if ( is_wp_error( $category ) )
34 _make_cat_compat($category);
39 function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
40 $category_path = rawurlencode(urldecode($category_path));
41 $category_path = str_replace('%2F', '/', $category_path);
42 $category_path = str_replace('%20', ' ', $category_path);
43 $category_paths = '/' . trim($category_path, '/');
44 $leaf_path = sanitize_title(basename($category_paths));
45 $category_paths = explode('/', $category_paths);
47 foreach ( (array) $category_paths as $pathdir )
48 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
50 $categories = get_terms('category', "get=all&slug=$leaf_path");
52 if ( empty($categories) )
55 foreach ($categories as $category) {
56 $path = '/' . $leaf_path;
57 $curcategory = $category;
58 while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
59 $curcategory = get_term($curcategory->parent, 'category');
60 if ( is_wp_error( $curcategory ) )
62 $path = '/' . $curcategory->slug . $path;
65 if ( $path == $full_path )
66 return get_category($category->term_id, $output);
69 // If full matching is not required, return the first cat that matches the leaf.
71 return get_category($categories[0]->term_id, $output);
76 function get_category_by_slug( $slug ) {
77 $category = get_term_by('slug', $slug, 'category');
79 _make_cat_compat($category);
84 // Get the ID of a category from its name
85 function get_cat_ID($cat_name='General') {
86 $cat = get_term_by('name', $cat_name, 'category');
93 function get_catname($cat_ID) {
94 return get_cat_name($cat_ID);
97 // Get the name of a category from its ID
98 function get_cat_name($cat_id) {
99 $cat_id = (int) $cat_id;
100 $category = &get_category($cat_id);
101 return $category->name;
104 function cat_is_ancestor_of($cat1, $cat2) {
106 $cat1 = & get_category($cat1);
108 $cat2 = & get_category($cat2);
110 if ( !$cat1->term_id || !$cat2->parent )
113 if ( $cat2->parent == $cat1->term_id )
116 return cat_is_ancestor_of($cat1, get_category($cat2->parent));
119 function sanitize_category($category, $context = 'display') {
120 return sanitize_term($category, 'category', $context);
123 function sanitize_category_field($field, $value, $cat_id, $context) {
124 return sanitize_term_field($field, $value, $cat_id, 'category', $context);
129 function &get_tags($args = '') {
130 $tags = get_terms('post_tag', $args);
135 $tags = apply_filters('get_tags', $tags, $args);
139 function &get_tag($tag, $output = OBJECT, $filter = 'raw') {
140 return get_term($tag, 'post_tag', $output, $filter);
147 function update_category_cache() {
151 function clean_category_cache($id) {
152 clean_term_cache($id, 'category');
159 function _make_cat_compat( &$category) {
160 if ( is_object($category) ) {
161 $category->cat_ID = &$category->term_id;
162 $category->category_count = &$category->count;
163 $category->category_description = &$category->description;
164 $category->cat_name = &$category->name;
165 $category->category_nicename = &$category->slug;
166 $category->category_parent = &$category->parent;
167 } else if ( is_array($category) && isset($category['term_id']) ) {
168 $category['cat_ID'] = &$category['term_id'];
169 $category['category_count'] = &$category['count'];
170 $category['category_description'] = &$category['description'];
171 $category['cat_name'] = &$category['name'];
172 $category['category_nicename'] = &$category['slug'];
173 $category['category_parent'] = &$category['parent'];