X

Automatically categorize and tag posts when saved

Adding this snippet to the functions.php of your wordpress theme will let you automatically categorize and tag posts when you save them. Don't forget to add a new tag or a new category


( example screenshot )


Code Snippet:


<?php

add_action( 'wp_insert_post', 'update_post_terms' );

function update_post_terms( $post_id ) {
if ( $parent = wp_is_post_revision( $post_id ) )
$post_id = $parent;
$post = get_post( $post_id );
if ( $post->post_type != 'post' )
return;

// add a tag
wp_set_post_terms( $post_id, 'new tag', 'post_tag', true );

// add a category
$categories = wp_get_post_categories( $post_id );
$newcat = get_term_by( 'name', 'Some Category', 'category' );

array_push( $categories, $newcat->term_id );
wp_set_post_categories( $post_id, $categories );
}
?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!