3 * WordPress Category API
9 * Retrieves all category IDs.
12 * @link http://codex.wordpress.org/Function_Reference/get_all_category_ids
14 * @return object List of all of the category IDs.
16 function get_all_category_ids() {
17 if ( ! $cat_ids = wp_cache_get( 'all_category_ids', 'category' ) ) {
18 $cat_ids = get_terms( 'category', array('fields' => 'ids', 'get' => 'all') );
19 wp_cache_add( 'all_category_ids', $cat_ids, 'category' );
26 * Retrieve list of category objects.
28 * If you change the type to 'link' in the arguments, then the link categories
29 * will be returned instead. Also all categories will be updated to be backwards
30 * compatible with pre-2.3 plugins and themes.
33 * @see get_terms() Type of arguments that can be changed.
34 * @link http://codex.wordpress.org/Function_Reference/get_categories
36 * @param string|array $args Optional. Change the defaults retrieving categories.
37 * @return array List of categories.
39 function get_categories( $args = '' ) {
40 $defaults = array( 'taxonomy' => 'category' );
41 $args = wp_parse_args( $args, $defaults );
43 $taxonomy = $args['taxonomy'];
45 * Filter the taxonomy used to retrieve terms when calling get_categories().
49 * @param string $taxonomy Taxonomy to retrieve terms from.
50 * @param array $args An array of arguments. @see get_terms()
52 $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
55 if ( isset($args['type']) && 'link' == $args['type'] ) {
56 _deprecated_argument( __FUNCTION__, '3.0', '' );
57 $taxonomy = $args['taxonomy'] = 'link_category';
60 $categories = (array) get_terms( $taxonomy, $args );
62 foreach ( array_keys( $categories ) as $k )
63 _make_cat_compat( $categories[$k] );
69 * Retrieves category data given a category ID or category object.
71 * If you pass the $category parameter an object, which is assumed to be the
72 * category row object retrieved the database. It will cache the category data.
74 * If you pass $category an integer of the category ID, then that category will
75 * be retrieved from the database, if it isn't already cached, and pass it back.
77 * If you look at get_term(), then both types will be passed through several
78 * filters and finally sanitized based on the $filter parameter value.
80 * The category will converted to maintain backwards compatibility.
83 * @uses get_term() Used to get the category data from the taxonomy.
85 * @param int|object $category Category ID or Category row object
86 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
87 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
88 * @return mixed Category data in type defined by $output parameter.
90 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
91 $category = get_term( $category, 'category', $output, $filter );
92 if ( is_wp_error( $category ) )
95 _make_cat_compat( $category );
101 * Retrieve category based on URL containing the category slug.
103 * Breaks the $category_path parameter up to get the category slug.
105 * Tries to find the child path and will return it. If it doesn't find a
106 * match, then it will return the first category matching slug, if $full_match,
107 * is set to false. If it does not, then it will return null.
109 * It is also possible that it will return a WP_Error object on failure. Check
110 * for it when using this function.
114 * @param string $category_path URL containing category slugs.
115 * @param bool $full_match Optional. Whether full path should be matched.
116 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
117 * @return null|object|array Null on failure. Type is based on $output value.
119 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
120 $category_path = rawurlencode( urldecode( $category_path ) );
121 $category_path = str_replace( '%2F', '/', $category_path );
122 $category_path = str_replace( '%20', ' ', $category_path );
123 $category_paths = '/' . trim( $category_path, '/' );
124 $leaf_path = sanitize_title( basename( $category_paths ) );
125 $category_paths = explode( '/', $category_paths );
127 foreach ( (array) $category_paths as $pathdir )
128 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
130 $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
132 if ( empty( $categories ) )
135 foreach ( $categories as $category ) {
136 $path = '/' . $leaf_path;
137 $curcategory = $category;
138 while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
139 $curcategory = get_term( $curcategory->parent, 'category' );
140 if ( is_wp_error( $curcategory ) )
142 $path = '/' . $curcategory->slug . $path;
145 if ( $path == $full_path ) {
146 $category = get_term( $category->term_id, 'category', $output );
147 _make_cat_compat( $category );
152 // If full matching is not required, return the first cat that matches the leaf.
153 if ( ! $full_match ) {
154 $category = get_term( reset( $categories )->term_id, 'category', $output );
155 _make_cat_compat( $category );
163 * Retrieve category object by category slug.
167 * @param string $slug The category slug.
168 * @return object Category data object
170 function get_category_by_slug( $slug ) {
171 $category = get_term_by( 'slug', $slug, 'category' );
173 _make_cat_compat( $category );
179 * Retrieve the ID of a category from its name.
183 * @param string $cat_name Category name.
184 * @return int 0, if failure and ID of category on success.
186 function get_cat_ID( $cat_name ) {
187 $cat = get_term_by( 'name', $cat_name, 'category' );
189 return $cat->term_id;
194 * Retrieve the name of a category from its ID.
198 * @param int $cat_id Category ID
199 * @return string Category name, or an empty string if category doesn't exist.
201 function get_cat_name( $cat_id ) {
202 $cat_id = (int) $cat_id;
203 $category = get_term( $cat_id, 'category' );
204 if ( ! $category || is_wp_error( $category ) )
206 return $category->name;
210 * Check if a category is an ancestor of another category.
212 * You can use either an id or the category object for both parameters. If you
213 * use an integer the category will be retrieved.
217 * @param int|object $cat1 ID or object to check if this is the parent category.
218 * @param int|object $cat2 The child category.
219 * @return bool Whether $cat2 is child of $cat1
221 function cat_is_ancestor_of( $cat1, $cat2 ) {
222 return term_is_ancestor_of( $cat1, $cat2, 'category' );
226 * Sanitizes category data based on context.
229 * @uses sanitize_term() See this function for what context are supported.
231 * @param object|array $category Category data
232 * @param string $context Optional. Default is 'display'.
233 * @return object|array Same type as $category with sanitized data for safe use.
235 function sanitize_category( $category, $context = 'display' ) {
236 return sanitize_term( $category, 'category', $context );
240 * Sanitizes data in single category key field.
243 * @uses sanitize_term_field() See function for more details.
245 * @param string $field Category key to sanitize
246 * @param mixed $value Category value to sanitize
247 * @param int $cat_id Category ID
248 * @param string $context What filter to use, 'raw', 'display', etc.
249 * @return mixed Same type as $value after $value has been sanitized.
251 function sanitize_category_field( $field, $value, $cat_id, $context ) {
252 return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
258 * Retrieves all post tags.
261 * @see get_terms() For list of arguments to pass.
262 * @uses apply_filters() Calls 'get_tags' hook on array of tags and with $args.
264 * @param string|array $args Tag arguments to use when retrieving tags.
265 * @return array List of tags.
267 function get_tags( $args = '' ) {
268 $tags = get_terms( 'post_tag', $args );
270 if ( empty( $tags ) ) {
276 * Filter the array of term objects returned for the 'post_tag' taxonomy.
280 * @param array $tags Array of 'post_tag' term objects.
281 * @param array $args An array of arguments. @see get_terms()
283 $tags = apply_filters( 'get_tags', $tags, $args );
288 * Retrieve post tag by tag ID or tag object.
290 * If you pass the $tag parameter an object, which is assumed to be the tag row
291 * object retrieved the database. It will cache the tag data.
293 * If you pass $tag an integer of the tag ID, then that tag will
294 * be retrieved from the database, if it isn't already cached, and pass it back.
296 * If you look at get_term(), then both types will be passed through several
297 * filters and finally sanitized based on the $filter parameter value.
301 * @param int|object $tag
302 * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
303 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
304 * @return object|array Return type based on $output value.
306 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
307 return get_term( $tag, 'post_tag', $output, $filter );
313 * Remove the category cache data based on ID.
316 * @uses clean_term_cache() Clears the cache for the category based on ID
318 * @param int $id Category ID
320 function clean_category_cache( $id ) {
321 clean_term_cache( $id, 'category' );
325 * Update category structure to old pre 2.3 from new taxonomy structure.
327 * This function was added for the taxonomy support to update the new category
328 * structure with the old category one. This will maintain compatibility with
329 * plugins and themes which depend on the old key or property names.
331 * The parameter should only be passed a variable and not create the array or
332 * object inline to the parameter. The reason for this is that parameter is
333 * passed by reference and PHP will fail unless it has the variable.
335 * There is no return value, because everything is updated on the variable you
336 * pass to it. This is one of the features with using pass by reference in PHP.
341 * @param array|object $category Category Row object or array
343 function _make_cat_compat( &$category ) {
344 if ( is_object( $category ) ) {
345 $category->cat_ID = &$category->term_id;
346 $category->category_count = &$category->count;
347 $category->category_description = &$category->description;
348 $category->cat_name = &$category->name;
349 $category->category_nicename = &$category->slug;
350 $category->category_parent = &$category->parent;
351 } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
352 $category['cat_ID'] = &$category['term_id'];
353 $category['category_count'] = &$category['count'];
354 $category['category_description'] = &$category['description'];
355 $category['cat_name'] = &$category['name'];
356 $category['category_nicename'] = &$category['slug'];
357 $category['category_parent'] = &$category['parent'];