How to Automatically Categorize and Tag Posts in WordPress

Home » Snippets » How to Automatically Categorize and Tag Posts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you tired of manually categorizing and tagging each post on your WordPress website? Do you wish there was a way to automatically assign categories and tags based on the content of your posts? Well, good news! In this article, we will explore how you can automate the process of categorizing and tagging your posts, saving you time and effort. Whether you have a large blog or a small website, this technique will help you streamline your content organization and improve the user experience on your site. So, let’s dive in and learn how to automatically categorize and tag posts when they are saved in WordPress.

					function wpturbo_auto_categorize_and_tag_posts( $post_id ) {
    // Check if this is a new post
    if ( ! wp_is_post_revision( $post_id ) ) {
        // Get the post title
        $post_title = get_the_title( $post_id );
        
        // Get the post content
        $post_content = get_the_content( $post_id );
        
        // Get the categories and tags
        $categories = array( 'Category 1', 'Category 2' ); // Customize to your own categories
        $tags = array( 'Tag 1', 'Tag 2' ); // Customize to your own tags
        
        // Set the categories and tags for the post
        wp_set_post_categories( $post_id, $categories );
        wp_set_post_tags( $post_id, $tags );
    }
}
add_action( 'save_post', 'wpturbo_auto_categorize_and_tag_posts' );
				

The code snippet above creates a function called wpturbo_auto_categorize_and_tag_posts() that automatically categorizes and tags a post when it is saved. This is useful for automatically organizing and tagging posts based on specific criteria.

First, the function checks if the post is new by using the wp_is_post_revision() function. This ensures that the categorization and tagging process only runs when a new post is saved, and not when a post is edited or updated.

Next, the function retrieves the post title and content using the get_the_title() and get_the_content() functions, respectively. These functions retrieve the respective data for the post being saved, which will be used to determine the appropriate categories and tags.

Then, we define the categories and tags that we want to assign to the post. These can be customized by modifying the arrays $categories and $tags. You can add or remove categories and tags as per your requirements.

Finally, we use the wp_set_post_categories() and wp_set_post_tags() functions to assign the categories and tags to the post. The first function takes in two parameters: the post ID and an array of category IDs. The second function also takes in two parameters: the post ID and an array of tag names. In this example, we are assigning the predefined categories and tags to the post.

To make this categorization and tagging process happen automatically whenever a post is saved, we use the add_action() function to hook the wpturbo_auto_categorize_and_tag_posts() function to the save_post action.

Overall, this code snippet allows you to automatically categorize and tag posts based on specific criteria, saving you time and effort in organizing and managing your content.

Register an account to save your snippets or go Pro to get more features.