Using wp_set_post_categories to set categories to a post in WordPress

The wp_set_post_categories function in WordPress is designed to set the categories for a post. This function is part of the WordPress core and is used to assign one or more categories to a specific post.

Using this function, the categories of a post can be programmatically changed. This is particularly useful when there is a need to update the categories of a post dynamically, without manual intervention. The function replaces the existing categories assigned to a post with the new set of categories provided.

The function operates by taking the ID of the post and an array of category IDs. It then updates the post’s categories in the WordPress database.

It’s important to note that this function does not append categories. Instead, it replaces the current categories with the new ones provided. If the intention is to keep the existing categories and add new ones, this function may not be the right choice.

The wp_set_post_categories function provides a way to programmatically control the categories of a post in WordPress.

Parameters Accepted by wp_set_post_categories Function

The wp_set_post_categories function in WordPress accepts three parameters. These parameters are optional and have default values.

  • $post_id (int): This parameter is used to specify the Post ID. It does not automatically take the ID of the global $post and its default value is 0.
  • $post_categories (int[]int): This parameter is used to provide a list of category IDs or the ID of a single category. Its default value is an empty array.
  • $append (bool): This parameter determines whether to keep existing categories or replace them with new ones. If set to true, the function will not delete existing categories but will add new ones to the list. If set to false, the function will replace the existing categories with the new ones. Its default value is false.

Return Value of wp_set_post_categories Function

The wp_set_post_categories function returns an array of term taxonomy IDs of the affected categories. In case of failure, it returns either false or WP_Error.

Examples

How to Set Post Categories for a Specific Post

This is a basic usage of the wp_set_post_categories function to set categories for a specific post.

$post_id = 123; // The ID of the post you want to set categories for
$categories = array(1, 2, 3); // The IDs of the categories you want to set

$result = wp_set_post_categories($post_id, $categories);

In this example, we are setting the categories with IDs 1, 2, and 3 for the post with ID 123.

How to Replace Existing Categories for a Post

This is an example of using the wp_set_post_categories function to replace the existing categories of a post with new ones.

$post_id = 123; // The ID of the post you want to set categories for
$categories = array(4, 5, 6); // The IDs of the categories you want to set

$result = wp_set_post_categories($post_id, $categories, false);

In this example, we are replacing the existing categories of the post with ID 123 with the categories with IDs 4, 5, and 6. The third parameter of the wp_set_post_categories function is set to false, which means that the existing categories will be replaced, not appended.

How to Remove All Categories from a Post

This is an example of using the wp_set_post_categories function to remove all categories from a post.

$post_id = 123; // The ID of the post you want to remove categories from

$result = wp_set_post_categories($post_id, array());

In this example, we are removing all categories from the post with ID 123. We do this by passing an empty array as the second parameter to the wp_set_post_categories function. This will remove all categories from the post.

Conclusion

The wp_set_post_categories function in WordPress is an essential feature that allows developers to programmatically assign categories to a post. This function is particularly useful in scenarios where there is a need to automate or bulk update the categorization of posts, such as during a website migration or a major content restructuring. By leveraging the wp_set_post_categories function, developers can efficiently manage the categorization of posts, thereby enhancing the organization and discoverability of content on a WordPress site.

Related WordPress Functions