get_category

Retrieves category data given a category ID or category object.

Description

(object|array|WP_Error|null) get_category( (int|object) $category, (constant) $output = OBJECT, (string) $filter = 'raw' ); 

If you pass the $category parameter an object, which is assumed to be the category row object retrieved the database. It will cache the category data.

If you pass $category an integer of the category ID, then that category will be retrieved from the database, if it isn't already cached, and pass it back.

If you look at get_term(), then both types will be passed through several filters and finally sanitized based on the $filter parameter value.

The category will converted to maintain backward compatibility.

Returns (object|array|WP_Error|null)

Category data in type defined by $output parameter. WP_Error if $category is empty, null if it does not exist.

Parameters (3)

0. $category (int|object)
Category ID or Category row object
1. $output — Optional. (constant) => OBJECT
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT.
2. $filter — Optional. (string) => 'raw'
Default is raw or no WordPress defined filter will applied.

Usage

  1. if ( !function_exists( 'get_category' ) ) { 
  2. require_once ABSPATH . WPINC . '/category.php'; 
  3.  
  4. // Category ID or Category row object 
  5. $category = null; 
  6.  
  7. // Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to a 
  8. // WP_Term object, an associative array, or a numeric array, respectively. Default OBJECT. 
  9. $output = OBJECT; 
  10.  
  11. // Optional. Default is raw or no WordPress defined filter will applied. 
  12. $filter = 'raw'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_category($category, $output, $filter); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/category.php  
  1. function get_category( $category, $output = OBJECT, $filter = 'raw' ) { 
  2. $category = get_term( $category, 'category', $output, $filter ); 
  3.  
  4. if ( is_wp_error( $category ) ) 
  5. return $category; 
  6.  
  7. _make_cat_compat( $category ); 
  8.  
  9. return $category;