How to set WordPress Categories depending on the Post Title

I was working on a project the other day that required to determine which category a new post would go into, depending on the post title. This was important because posts were automatically acquired without the web interface, and in this workflow there was no way to pick a category other than the default.

In our case we wanted to use a post fix in the title to determine which category was to be picked: for example, if a post title would end with “_ONE”, it should end up in Category 1, and if it ends up with “_TWO” it would end up in Category 2.

Thankfully there’s a hook called save_post that is called every time a post is updated. At this point we can check what the post title is, determine the post fix, and set the correct category. Here’s a function that does just that:

function set_my_categories($post_ID){
	// grab the $post_ID
	if(wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
	  return $post_ID;
	// grab the current title
	$title = get_the_title($post_ID);
	// set the category depending on the last four characters of the title
	// _ONE = Category 1
	// _TWO = Category 2
	$postfix = substr($title, -4);
	if ($postfix == '_ONE') {
		wp_set_post_categories($post_ID, array(1));
	if ($postfix == '_TWO') {
		wp_set_post_categories($post_ID, array(2));
add_action('save_post', 'set_my_categories');

If a post fix is not present, the categories will not be changed.

For this example I’m assuming that my categories are actually 1 and 2 on the system, something that’s not really the case. To determine the correct value set under wp_set_post_categories, I usually head over to Posts – Categories, select the category I want to use and check the URL of what WordPress gives me. Say the URL looks like this:

then the tag_ID parameter hints that my category has an ID of 366, and that’s the value we need to use.

And if a post needs to go into two categories, separate the category IDs with a comma like so:

wp_set_post_categories($post_ID, array(2,3));

Removing the post fix before the title is displayed

Since our post fix is for internal purposes only, we may not want it to appear as part of the actual post title on the front page. But we also don’t want to remove it from every post once the category has been set and still have a reference in the admin interface. So the way to do it is to simply suppress it before out theme prints it out.

The following code will do just that: if a post fix is present, curb the title. If not, leave the title unchanged.

// curb title if we have a post fix
$title = get_the_title();
$postfix = substr($title, -4);
if ($postfix == '_ONE' || $postfix == '_TWO') {
	$title = substr($title, 0, -4);
// use echo $title to print the post title in your theme

And there we have it. The same principle can be used if your title contains a certain keyword and you want to use it to add the post to a particular category automatically.

You can leave a comment on my original post.