disc0dancer
disc0dancer

Reputation: 9365

How to check if a post belongs to a category in Wordpress

I need to check if a certain post belongs to a category or belongs to a descendant of a certain category. Is there a WordPress function for this, or do I need to do it myself?

I should have mentioned that the WP version is 2.7.

Upvotes: 2

Views: 19972

Answers (2)

Vicky Biswas
Vicky Biswas

Reputation: 121

For 2.7 or above use in_category($category, $_post)

Upvotes: 12

rz.
rz.

Reputation: 20037

$post = get_post($id);
$category = $post->$post_category;
cat_is_ancestor_of(($parent_category, $category));

For more info see the get_post and cat is ancestor of references. Also, while somewhat difficult to find this is a good place to start for most wp development related things.

Upvotes: 10

Related Questions