is_object_in_term()
Determine if the given object is associated with any of the given terms.
The given terms are checked against the object's terms' term_ids, names and slugs. Terms given as integers will only be checked against the object's terms' term_ids. If no terms are given, determines if object is associated with any terms in the given taxonomy.
Used By: has_term()
1 time — 0.000809 sec (slow) | 50000 times — 1.16 sec (fast) | PHP 7.0.8, WP 4.6.1
No Hooks.
Return
true|false|WP_Error
. WP_Error on input error.
Usage
is_object_in_term( $object_id, $taxonomy, $terms );
- $object_id(int) (required)
- ID of the object (post ID, link ID, ...).
- $taxonomy(string) (required)
- Single taxonomy name.
- $terms(int|string|int[]|string[])
- Term ID, name, slug, or array of such to check against.
Default: null
Examples
#1 Checking if there are terms for the object (post)
Let's check if the current post ($post->ID) is in the term 'Languages' (slug=langs or ID=5), taxonomy 'my_taxonomy':
$is_in = is_object_in_term( $post->ID, 'my_taxonomy', 'langs' ); if ( $is_in ) { echo 'The post is in the term Languages'; } // it is also possible to specify the term name (not slug) $is_in = is_object_in_term( $post->ID, 'my_taxonomy', 'Languages' ); // or ID $is_in = is_object_in_term( $post->ID, 'my_taxonomy', 5 );
#2 Multiple term check
$is_in = is_object_in_term( $post->ID, 'my_taxonomy', [ 'Languages', 25 ] ); if( $is_in ){ echo 'The post is related to one of the terms: Languages', 25'; }
Changelog
Since 2.7.0 | Introduced. |