3 * Taxonomy API: Core category-specific functionality
10 * Retrieve list of category objects.
12 * If you change the type to 'link' in the arguments, then the link categories
13 * will be returned instead. Also all categories will be updated to be backward
14 * compatible with pre-2.3 plugins and themes.
17 * @see get_terms() Type of arguments that can be changed.
19 * @param string|array $args {
20 * Optional. Arguments to retrieve categories. See get_terms() for additional options.
22 * @type string $taxonomy Taxonomy to retrieve terms for. In this case, default 'category'.
24 * @return array List of categories.
26 function get_categories( $args = '' ) {
29 if (!$post = get_post()) return false;
31 /* $defaults = array( 'taxonomy' => 'category' );
32 $args = wp_parse_args( $args, $defaults );
34 $taxonomy = $args['taxonomy'];
37 * Filters the taxonomy used to retrieve terms when calling get_categories().
41 * @param string $taxonomy Taxonomy to retrieve terms from.
42 * @param array $args An array of arguments. See get_terms().
44 // $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );
47 /* if ( isset($args['type']) && 'link' == $args['type'] ) {
48 // translators: 1: "type => link", 2: "taxonomy => link_category" alternative
49 _deprecated_argument( __FUNCTION__, '3.0.0',
50 sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
51 '<code>type => link</code>',
52 '<code>taxonomy => link_category</code>'
55 $taxonomy = $args['taxonomy'] = 'link_category';
58 $categories = get_terms( $taxonomy, $args );*/
61 $categories = $gContentApi->getCategory($post->ID);
63 if ( is_wp_error( $categories ) ) {
64 $categories = array();
66 $categories = (array) $categories;
67 foreach ( array_keys( $categories ) as $k ) {
68 _make_cat_compat( $categories[ $k ] );
76 * Retrieves category data given a category ID or category object.
78 * If you pass the $category parameter an object, which is assumed to be the
79 * category row object retrieved the database. It will cache the category data.
81 * If you pass $category an integer of the category ID, then that category will
82 * be retrieved from the database, if it isn't already cached, and pass it back.
84 * If you look at get_term(), then both types will be passed through several
85 * filters and finally sanitized based on the $filter parameter value.
87 * The category will converted to maintain backward compatibility.
91 * @param int|object $category Category ID or Category row object
92 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a
93 * WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
94 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
95 * @return object|array|WP_Error|null Category data in type defined by $output parameter.
96 * WP_Error if $category is empty, null if it does not exist.
98 function get_category( $category, $output = OBJECT, $filter = 'raw' ) {
99 $category = get_term( $category, 'category', $output, $filter );
101 if ( is_wp_error( $category ) )
104 _make_cat_compat( $category );
110 * Retrieve category based on URL containing the category slug.
112 * Breaks the $category_path parameter up to get the category slug.
114 * Tries to find the child path and will return it. If it doesn't find a
115 * match, then it will return the first category matching slug, if $full_match,
116 * is set to false. If it does not, then it will return null.
118 * It is also possible that it will return a WP_Error object on failure. Check
119 * for it when using this function.
123 * @param string $category_path URL containing category slugs.
124 * @param bool $full_match Optional. Whether full path should be matched.
125 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
126 * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
127 * @return WP_Term|array|WP_Error|null Type is based on $output value.
129 function get_category_by_path( $category_path, $full_match = true, $output = OBJECT ) {
130 $category_path = rawurlencode( urldecode( $category_path ) );
131 $category_path = str_replace( '%2F', '/', $category_path );
132 $category_path = str_replace( '%20', ' ', $category_path );
133 $category_paths = '/' . trim( $category_path, '/' );
134 $leaf_path = sanitize_title( basename( $category_paths ) );
135 $category_paths = explode( '/', $category_paths );
137 foreach ( (array) $category_paths as $pathdir ) {
138 $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir );
140 $categories = get_terms( 'category', array('get' => 'all', 'slug' => $leaf_path) );
142 if ( empty( $categories ) ) {
146 foreach ( $categories as $category ) {
147 $path = '/' . $leaf_path;
148 $curcategory = $category;
149 while ( ( $curcategory->parent != 0 ) && ( $curcategory->parent != $curcategory->term_id ) ) {
150 $curcategory = get_term( $curcategory->parent, 'category' );
151 if ( is_wp_error( $curcategory ) ) {
154 $path = '/' . $curcategory->slug . $path;
157 if ( $path == $full_path ) {
158 $category = get_term( $category->term_id, 'category', $output );
159 _make_cat_compat( $category );
164 // If full matching is not required, return the first cat that matches the leaf.
165 if ( ! $full_match ) {
166 $category = get_term( reset( $categories )->term_id, 'category', $output );
167 _make_cat_compat( $category );
173 * Retrieve category object by category slug.
177 * @param string $slug The category slug.
178 * @return object Category data object
180 function get_category_by_slug( $slug ) {
181 $category = get_term_by( 'slug', $slug, 'category' );
183 _make_cat_compat( $category );
189 * Retrieve the ID of a category from its name.
193 * @param string $cat_name Category name.
194 * @return int 0, if failure and ID of category on success.
196 function get_cat_ID( $cat_name ) {
197 $cat = get_term_by( 'name', $cat_name, 'category' );
199 return $cat->term_id;
204 * Retrieve the name of a category from its ID.
208 * @param int $cat_id Category ID
209 * @return string Category name, or an empty string if category doesn't exist.
211 function get_cat_name( $cat_id ) {
212 $cat_id = (int) $cat_id;
213 $category = get_term( $cat_id, 'category' );
214 if ( ! $category || is_wp_error( $category ) )
216 return $category->name;
220 * Check if a category is an ancestor of another category.
222 * You can use either an id or the category object for both parameters. If you
223 * use an integer the category will be retrieved.
227 * @param int|object $cat1 ID or object to check if this is the parent category.
228 * @param int|object $cat2 The child category.
229 * @return bool Whether $cat2 is child of $cat1
231 function cat_is_ancestor_of( $cat1, $cat2 ) {
232 return term_is_ancestor_of( $cat1, $cat2, 'category' );
236 * Sanitizes category data based on context.
240 * @param object|array $category Category data
241 * @param string $context Optional. Default is 'display'.
242 * @return object|array Same type as $category with sanitized data for safe use.
244 function sanitize_category( $category, $context = 'display' ) {
245 return sanitize_term( $category, 'category', $context );
249 * Sanitizes data in single category key field.
253 * @param string $field Category key to sanitize
254 * @param mixed $value Category value to sanitize
255 * @param int $cat_id Category ID
256 * @param string $context What filter to use, 'raw', 'display', etc.
257 * @return mixed Same type as $value after $value has been sanitized.
259 function sanitize_category_field( $field, $value, $cat_id, $context ) {
260 return sanitize_term_field( $field, $value, $cat_id, 'category', $context );
266 * Retrieves all post tags.
269 * @see get_terms() For list of arguments to pass.
271 * @param string|array $args Tag arguments to use when retrieving tags.
272 * @return array List of tags.
274 function get_tags( $args = '' ) {
275 $tags = get_terms( 'post_tag', $args );
277 if ( empty( $tags ) ) {
283 * Filters the array of term objects returned for the 'post_tag' taxonomy.
287 * @param array $tags Array of 'post_tag' term objects.
288 * @param array $args An array of arguments. @see get_terms()
290 $tags = apply_filters( 'get_tags', $tags, $args );
295 * Retrieve post tag by tag ID or tag object.
297 * If you pass the $tag parameter an object, which is assumed to be the tag row
298 * object retrieved the database. It will cache the tag data.
300 * If you pass $tag an integer of the tag ID, then that tag will
301 * be retrieved from the database, if it isn't already cached, and pass it back.
303 * If you look at get_term(), then both types will be passed through several
304 * filters and finally sanitized based on the $filter parameter value.
308 * @param int|WP_Term|object $tag A tag ID or object.
309 * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to
310 * a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
311 * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
312 * @return WP_Term|array|WP_Error|null Tag data in type defined by $output parameter. WP_Error if $tag is empty, null if it does not exist.
314 function get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
315 return get_term( $tag, 'post_tag', $output, $filter );
321 * Remove the category cache data based on ID.
325 * @param int $id Category ID
327 function clean_category_cache( $id ) {
328 clean_term_cache( $id, 'category' );
332 * Update category structure to old pre 2.3 from new taxonomy structure.
334 * This function was added for the taxonomy support to update the new category
335 * structure with the old category one. This will maintain compatibility with
336 * plugins and themes which depend on the old key or property names.
338 * The parameter should only be passed a variable and not create the array or
339 * object inline to the parameter. The reason for this is that parameter is
340 * passed by reference and PHP will fail unless it has the variable.
342 * There is no return value, because everything is updated on the variable you
343 * pass to it. This is one of the features with using pass by reference in PHP.
346 * @since 4.4.0 The `$category` parameter now also accepts a WP_Term object.
349 * @param array|object|WP_Term $category Category Row object or array
351 function _make_cat_compat( &$category ) {
352 if ( is_object( $category ) && ! is_wp_error( $category ) ) {
353 $category->cat_ID = $category->term_id;
354 $category->category_count = $category->count;
355 $category->category_description = $category->description;
356 $category->cat_name = $category->name;
357 $category->category_nicename = $category->slug;
358 $category->category_parent = $category->parent;
359 } elseif ( is_array( $category ) && isset( $category['term_id'] ) ) {
360 $category['cat_ID'] = &$category['term_id'];
361 $category['category_count'] = &$category['count'];
362 $category['category_description'] = &$category['description'];
363 $category['cat_name'] = &$category['name'];
364 $category['category_nicename'] = &$category['slug'];
365 $category['category_parent'] = &$category['parent'];