Check if a post is assigned to a term with is_object_in_term in WordPress

The is_object_in_term function in WordPress is used to check if an object (like a post or a term) is associated with a specific term in a taxonomy. This function can be beneficial in various scenarios where there is a need to verify the relationship between objects and terms in a taxonomy. For instance, this function can be used to determine if a post is in a specific category or not.

It is important to note that the is_object_in_term function does not directly return a boolean value. Instead, it returns an array of term IDs if the object is in the term. If the object is not associated with the term, it will return false. In case of an error, it will return a WP_Error object.

While using this function, it is crucial to be aware of its limitations. For example, it does not work with hierarchical taxonomies. If you need to check if an object is in a term of a hierarchical taxonomy, you would need to use another function.

Parameters Accepted by the is_object_in_term Function

The is_object_in_term function in WordPress accepts three parameters. These parameters provide the function with the necessary information to perform its task.

  • $object_id (integer): This is a mandatory parameter. It is used to specify the ID of the object. The object could be a post ID, link ID, etc.
  • $taxonomy (string): This is also a required parameter. It is used to specify the name of a single taxonomy.
  • $terms (integer, string, integer array, string array): This parameter is optional and its default value is null. It is used to specify the term ID, name, slug, or an array of such to check against.

Return Value of the is_object_in_term Function

The is_object_in_term function returns either a boolean value or a WP_Error. This means that the function will return a true or false value, depending on whether the object is in the term or not. If there is an error in the input, the function will return a WP_Error.

Examples

How to Check if a Post is in a Specific Category

$post_id = 123; // Replace with your post ID
$category = 'news'; // Replace with your category

if (is_object_in_term($post_id, 'category', $category)) {
 echo '<p>The post is in the News category.</p>';
} else {
 echo '<p>The post is not in the News category.</p>';
}

This code snippet checks if the post with ID $post_id is in the ‘news’ category. If it is, it outputs a message saying “The post is in the News category.” If it’s not, it outputs “The post is not in the News category.”

How to Check if a Post is in Any of Multiple Categories

$post_id = 123; // Replace with your post ID
$categories = array('news', 'updates'); // Replace with your categories

if (is_object_in_term($post_id, 'category', $categories)) {
 echo '<p>The post is in either the News or Updates category.</p>';
} else {
 echo '<p>The post is not in either the News or Updates category.</p>';
}

This code snippet checks if the post with ID $post_id is in either the ‘news’ or ‘updates’ category. If it is, it outputs a message saying “The post is in either the News or Updates category.” If it’s not, it outputs “The post is not in either the News or Updates category.”

How to Handle Errors When Checking if a Post is in a Category

$post_id = 123; // Replace with your post ID
$category = 'news'; // Replace with your category

$result = is_object_in_term($post_id, 'category', $category);

if (is_wp_error($result)) {
 echo '<p>An error occurred: ' . $result->get_error_message() . '</p>';
} elseif ($result) {
 echo '<p>The post is in the News category.</p>';
} else {
 echo '<p>The post is not in the News category.</p>';
}

This code snippet checks if the post with ID $post_id is in the ‘news’ category and handles any errors that might occur. If an error occurs, it outputs a message with the error. If no error occurs, it outputs a message saying whether the post is in the ‘news’ category.

Conclusion

The is_object_in_term function in WordPress is a tool that checks if an object, such as a post, is associated with a term in a taxonomy. This function can be used to determine whether a specific term is assigned to an object, which can be useful in a variety of scenarios, such as filtering posts based on their assigned categories or tags, or creating conditional statements in your theme or plugin code based on term assignments.

Related WordPress Functions