Title: in_category
Published: April 25, 2014
Last modified: February 24, 2026

---

# in_category( int|string|int[]|string[] $category, int|null|WP_Post $post = null ): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#wp--skip-link--target)

Checks if the current post is within any of the given categories.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#description)󠁿

The given categories are checked against the post’s categories’ term_ids, names 
and slugs.
Categories given as integers will only be checked against the post’s 
categories’ term_ids.

Prior to v2.5 of WordPress, category names were not supported.
Prior to v2.7, category
slugs were not supported.Prior to v2.7, only one category could be compared: in_category(
$single_category ).Prior to v2.7, this function could only be used in the WordPress
Loop.As of 2.7, the function can be used anywhere if it is provided a post ID or
post object.

For more information on this and similar theme functions, check out the [ Conditional Tags](https://developer.wordpress.org/themes/basics/conditional-tags/)
article in the Theme Developer Handbook.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#parameters)󠁿

 `$category`int|string|int[]|string[]required

Category ID, name, slug, or array of such to check against.

`$post`int|null|[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)
optional

Post to check. Defaults to the current post.

Default:`null`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#return)󠁿

 bool True if the current post is in any of the given categories.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#source)󠁿

    ```php
    function in_category( $category, $post = null ) {
    	if ( empty( $category ) ) {
    		return false;
    	}

    	return has_category( $category, $post );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/category-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/category-template.php#L257)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/category-template.php#L257-L263)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#related)󠁿

| Uses | Description | 
| [has_category()](https://developer.wordpress.org/reference/functions/has_category/)`wp-includes/category-template.php` |

Checks if the current post has any of given category.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.7.0](https://developer.wordpress.org/reference/since/2.7.0/) | The `$post` parameter was added. | 
| [1.2.0](https://developer.wordpress.org/reference/since/1.2.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#comment-content-488)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/in_category/#comment-488)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-488)
     Vote results for this note: 7[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-488)
 4.  **Testing the current post outside the Loop**
      During a request for an individual
     post (usually handled by the single.php template), you can test that post’s categories
     even before the Loop is begun.
 5.  You could use this to switch templates like so:
 6.      ```php
         if ( in_category('fruit') ) {
         	include 'single-fruit.php';
         } elseif ( in_category('vegetables') ) {
         	include 'single-vegetables.php';
         } else {
         	// Continue with normal Loop
         	if ( have_posts() ) : while ( have_posts() ) : the_post();
         	// ...
         }
         ```
     
 7.  (The Custom Post Templates Plugin allows for creation of templates for single 
     posts. It also shows an example of how to add a template which is used for all
     posts in a given category, not just a single post. That example is commented out
     in the plugin by default but can be easily implemented by uncommenting the appropriate
     lines.)
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%3Freplytocom%3D488%23feedback-editor-488)
 9.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#comment-content-487)
 10.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/in_category/#comment-487)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-487)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-487)
 12. **Testing the current post within the Loop**
      `in_category()` is often used to
     take different actions within the Loop depending on the current post’s category,
     e.g.
 13.     ```php
         if ( in_category( 'pachyderms' )) {
         	// They have long trunks...
         } elseif ( in_category( array( 'Tropical Birds', 'small-mammals' ) )) {
         	// They are warm-blooded...
         } else {
         	// etc.
         }
         ```
     
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%3Freplytocom%3D487%23feedback-editor-487)
 15.  [Skip to note 7 content](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#comment-content-4999)
 16.   [revive](https://profiles.wordpress.org/revive/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/in_category/#comment-4999)
 17. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-4999)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-4999)
 18. To check whether a post is within a parent category or any of it’s subcategories:
 19.     ```php
         <?php
         /**
          * Tests if any of a post's assigned categories are descendants of target categories
          *
          * @param int|array $categories The target categories. Integer ID or array of integer IDs
          * @param int|object $_post The post. Omit to test the current post in the Loop or main query
          * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
          * @see get_term_by() You can get a category by name or slug, then pass ID to this function
          * @uses get_term_children() Passes $cats
          * @uses in_category() Passes $_post (can be empty)
          * @link originally at https://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
          */
     
         if ( ! function_exists( 'wpdocs_post_is_in_a_subcategory' ) ) {
         function wpdocs_post_is_in_a_subcategory( $categories, $_post = null ) {
             foreach ( (array) $categories as $category ) {
                 // get_term_children() only accepts integer ID
                 $subcats = get_term_children( (int) $category, 'category' );
                 if ( $subcats && in_category( $subcats, $_post ) )
                     return true;
             }
             return false;
           }
         }
         ?>
         ```
     
 20. Use for checking if within a single cat:
 21.     ```php
         wpdocs_post_is_in_a_subcategory( 25 );
         ```
     
 22. Use for checking if within an array of parent categories, or any of their subcats:
 23.     ```php
         wpdocs_post_is_in_a_subcategory( array( 25, 28, 124, 297, 298, 299 ) );
         ```
     
 24.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%3Freplytocom%3D4999%23feedback-editor-4999)
 25.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/in_category/?output_format=md#comment-content-6852)
 26.   [Tunn](https://profiles.wordpress.org/iuriem/)  [  2 years ago  ](https://developer.wordpress.org/reference/functions/in_category/#comment-6852)
 27. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-6852)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%23comment-6852)
 28. It is not documented, but as I found in my tests, the _[in\_category()](https://developer.wordpress.org/reference/functions/in_category/)_
     function also accepts as a _$category_ parameter a _[WP\_Term](https://developer.wordpress.org/reference/classes/wp_term/)_
     object, returned for example by the _[get\_term()](https://developer.wordpress.org/reference/functions/get_term/)_
     function.
 29.  * Interesting. Reviewing the chain of functions used, I can’t determine how/why
        this works.
      * [crstauf](https://profiles.wordpress.org/crstauf/) [2 years ago](https://developer.wordpress.org/reference/functions/in_category/#comment-6856)
 30.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F%3Freplytocom%3D6852%23feedback-editor-6852)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fin_category%2F)
before being able to contribute a note or feedback.