3 function get_all_category_ids() {
6 if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
7 $cat_ids = get_terms('category', 'fields=ids&get=all');
8 wp_cache_add('all_category_ids', $cat_ids, 'category');
14 function &get_categories($args = '') {
15 $defaults = array('type' => 'category');
16 $args = wp_parse_args($args, $defaults);
18 $taxonomy = 'category';
19 if ( 'link' == $args['type'] )
20 $taxonomy = 'link_category';
21 $categories = get_terms($taxonomy, $args);
23 foreach ( array_keys($categories) as $k )
24 _make_cat_compat($categories[$k]);
29 // Retrieves category data given a category ID or category object.
30 // Handles category caching.
31 function &get_category($category, $output = OBJECT, $filter = 'raw') {
32 $category = get_term($category, 'category', $output, $filter);
33 if ( is_wp_error( $category ) )
36 _make_cat_compat($category);
41 function get_category_by_path($category_path, $full_match = true, $output = OBJECT) {
43 $category_path = rawurlencode(urldecode($category_path));
44 $category_path = str_replace('%2F', '/', $category_path);
45 $category_path = str_replace('%20', ' ', $category_path);
46 $category_paths = '/' . trim($category_path, '/');
47 $leaf_path = sanitize_title(basename($category_paths));
48 $category_paths = explode('/', $category_paths);
50 foreach ( (array) $category_paths as $pathdir )
51 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir);
53 $categories = get_terms('category', "get=all&slug=$leaf_path");
55 if ( empty($categories) )
58 foreach ($categories as $category) {
59 $path = '/' . $leaf_path;
60 $curcategory = $category;
61 while ( ($curcategory->parent != 0) && ($curcategory->parent != $curcategory->term_id) ) {
62 $curcategory = get_term($curcategory->parent, 'category');
63 if ( is_wp_error( $curcategory ) )
65 $path = '/' . $curcategory->slug . $path;
68 if ( $path == $full_path )
69 return get_category($category->term_id, $output);
72 // If full matching is not required, return the first cat that matches the leaf.
74 return get_category($categories[0]->term_id, $output);
79 function get_category_by_slug( $slug ) {
80 $category = get_term_by('slug', $slug, 'category');
82 _make_cat_compat($category);
87 // Get the ID of a category from its name
88 function get_cat_ID($cat_name='General') {
91 $cat = get_term_by('name', $cat_name, 'category');
98 function get_catname($cat_ID) {
99 return get_cat_name($cat_ID);
102 // Get the name of a category from its ID
103 function get_cat_name($cat_id) {
104 $cat_id = (int) $cat_id;
105 $category = &get_category($cat_id);
106 return $category->name;
109 function cat_is_ancestor_of($cat1, $cat2) {
111 $cat1 = & get_category($cat1);
113 $cat2 = & get_category($cat2);
115 if ( !$cat1->term_id || !$cat2->parent )
118 if ( $cat2->parent == $cat1->term_id )
121 return cat_is_ancestor_of($cat1, get_category($cat2->parent));
124 function sanitize_category($category, $context = 'display') {
125 return sanitize_term($category, 'category', $context);
128 function sanitize_category_field($field, $value, $cat_id, $context) {
129 return sanitize_term_field($field, $value, $cat_id, 'category', $context);
134 function &get_tags($args = '') {
135 global $wpdb, $category_links;
137 $key = md5( serialize( $args ) );
138 if ( $cache = wp_cache_get( 'get_tags', 'category' ) )
139 if ( isset( $cache[ $key ] ) )
140 return apply_filters('get_tags', $cache[$key], $args);
143 $tags = get_terms('post_tag', $args);
148 $cache[ $key ] = $tags;
149 wp_cache_set( 'get_tags', $cache, 'category' );
151 $tags = apply_filters('get_tags', $tags, $args);
155 function &get_tag($tag, $output = OBJECT, $filter = 'raw') {
156 return get_term($tag, 'post_tag', $output, $filter);
163 function update_category_cache() {
167 function clean_category_cache($id) {
168 clean_term_cache($id, 'category');
175 function _make_cat_compat( &$category) {
176 if ( is_object($category) ) {
177 $category->cat_ID = &$category->term_id;
178 $category->category_count = &$category->count;
179 $category->category_description = &$category->description;
180 $category->cat_name = &$category->name;
181 $category->category_nicename = &$category->slug;
182 $category->category_parent = &$category->parent;
183 } else if ( is_array($category) && isset($category['term_id']) ) {
184 $category['cat_ID'] = &$category['term_id'];
185 $category['category_count'] = &$category['count'];
186 $category['category_description'] = &$category['description'];
187 $category['cat_name'] = &$category['name'];
188 $category['category_nicename'] = &$category['slug'];
189 $category['category_parent'] = &$category['parent'];