has_category() WP 3.1.0
Checks whether the current or specified post has at least one of the specified categories.
Works for category
taxonomy only. Use has_term(), when you need to check a post for another taxonomy.
If no categories are provided for comparison, the function will check whether at least one category is assigned to a post.
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.
If no categories are given, determines if post has any categories.
Works based on: has_term()
Basis of: in_category()
1 time = 0.003414s = very slow | 50000 times = 0.84s = very fast | PHP 7.1.5, WP 4.8.1
No Hooks.
Return
true/false. True if the current post has any of the given categories (or any category, if no category specified). False otherwise.
Usage
has_category( $category, $post );
- $category(string/int/array)
- The category name/term_id/slug, or an array of them to check for.
Default: '' - $post(int/object)
- Post to check instead of the current post.
Default: null
Examples
#1 Check if the post at least one specified category
$post_id = 6235; if( has_category( array(37,'history'), $post_id) ){ echo "Post with ID = $post_id is assignet to the category `37` or `history`"; }
Changelog
Since 3.1.0 | Introduced. |
Code of has_category() has category WP 5.6
function has_category( $category = '', $post = null ) {
return has_term( $category, 'category', $post );
}Related Functions
From tag: Conditional tags (all)
- cat_is_ancestor_of()
- comments_open()
- email_exists()
- has_custom_header()
- has_excerpt()
- has_nav_menu()
- has_post_thumbnail()
- has_shortcode()
- has_tag()
- has_term()
- have_comments()
- have_posts()
- in_category()
- in_the_loop()
- is_404()
- is_active_sidebar()
- is_admin()
- is_admin_bar_showing()
- is_archive()
- is_attachment()
- is_author()
- is_blog_admin()
- is_blog_installed()
- is_category()
- is_child_theme()
- is_comment_feed()
- is_customize_preview()
- is_date()
- is_day()
- is_dynamic_sidebar()
- is_embed()
- is_feed()
- is_front_page()
- is_header_video_active()
- is_home()
- is_local_attachment()
- is_main_query()
- is_month()
- is_multi_author()
- is_multisite()
- is_nav_menu()
- is_network_admin()
- is_new_day()
- is_page()
- is_page_template()
- is_paged()
- is_plugin_active()
- is_post_type_archive()
- is_post_type_hierarchical()
- is_preview()
- is_robots()
- is_search()
- is_single()
- is_singular()
- is_ssl()
- is_sticky()
- is_tag()
- is_tax()
- is_taxonomy_hierarchical()
- is_textdomain_loaded()
- is_time()
- is_trackback()
- is_user_admin()
- is_user_logged_in()
- is_year()
- pings_open()
- post_exists()
- post_password_required()
- shortcode_exists()
- taxonomy_exists()
- term_exists()
- term_is_ancestor_of()
- wp_attachment_is()
- wp_attachment_is_image()
- wp_doing_ajax()
- wp_doing_cron()
- wp_is_mobile()
- wp_is_post_autosave()
- wp_is_post_revision()
- wp_script_is()
More from category: For posts
- get_object_taxonomies()
- get_objects_in_term()
- get_the_category()
- get_the_category_list()
- get_the_tag_list()
- get_the_tags()
- get_the_taxonomies()
- get_the_term_list()