You could try this. I havn’t really tested it, so I’m not sure it works – might require some small tweaks aswell.
function add_category_based_on_title( $post_id ) {
// title
$title = get_the_title( $post_id );
// get first part of title
$substr = explode( " ", $title )[0];
// get category id
$category_id = get_cat_ID( $substr );
// check category
if ( ! empty( $category_id ) ) ) {
// set category
wp_set_post_categories( $post_id, [ $category_id ], true )
}
}
add_action( 'save_post', 'add_category_based_on_title' );
References:
https://codex.wordpress.org/Function_Reference/wp_set_post_categories
https://codex.wordpress.org/Function_Reference/get_cat_ID
https://developer.wordpress.org/reference/functions/get_the_title/
Related Posts:
- How to display related posts from same category?
- How to show related posts by category
- How to add category to: ‘wp-admin/post-new.php’?
- Use category base slug in posts’ permalink
- Setting Custom Sort Order of Posts within a Category
- Display all posts from specific categories on a page
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- How to disable Uncategorized category URL?
- Add role that restricts user to post in specific category
- Count posts in category including child categories
- Add default content to post (for specific category)
- Query only Posts from Both of Two Category?
- Show Custom Taxonomy Categories, Listing of Posts, and Single Post via AJAX
- Display most recent post in category instead of archive?
- Archived Posts in Wrong Category
- get_post_type_archive_link(‘post’) returns current category archive
- Display all posts under child sub category in sidebar on post page?
- How do I get the slug of a custom taxonomy category of a post?
- Featured Posts for Category Pages
- Authors in menu, template list post by author
- Get user’s most published categories
- Conditional that checks if a current category has any subcategory with posts
- How to set post expiration date and time and move the page to archive after expiration [closed]
- Count to how many categories a post belongs to
- How can I remove posts of a certain category from homepage after a specified time period?
- Articles are text-only in any “Category” instead of HTML
- How To Remove Certain category Posts From the Blog Page
- Add icon/badge next to posttitle of specific category
- Display post X of Y in category
- Get current category id from post page wordpress
- Saving category to a post, before publishing the post
- Exclude category from
- Assign category to new post via URL
- Stop users posting into category while still seeing contents of the category
- Category Page Template not Showing Posts
- How to Restrict Previous & Next Post Link to Posts of Same Category?
- WordPress custom Query for Posts in Category display posts multiple times
- Categories list into registration form
- WordPress Multisite – Create Default Post and New Category On New Site Install
- Using ajax on editing a category edit page?
- Disable sticky option for specific categories
- How to get subcategories from category slug?
- How to display two random-post sections that are each under their own category
- Loop doesn’t exclude the specified category in home page
- How to create a sub post?
- Post Image not displaying in category view
- WordPress bulk category select when publishing post
- Exclude some categories from listing on the current post
- how can i display my posts alphabetically?
- Parent category / child category posts
- Change Post Title For Specific Category
- In what context should Categories and Tags be used
- list posts from two categories on a page – plugin not working
- How to create a playlist for audio associated with posts per category
- posts going to 2 pages/categories only show up in 1
- What’s a theme that properly handles previews of different types of posts?
- Display Parent Category of a Post belonging only to Subcategory
- Can I style single post that are in multiple catergories?
- How to add content above footer in posts from specific category
- Rewriting blog permalinks
- How to make ajax load posts from the current category?
- WordPress – When visiting a `single post/ single.php` my category menu item link becomes active
- Show single post child category from a determined parent
- How can I create a ‘sub-blog’ which has its own sidebar on individual posts?
- I want to add the 10 most recent posts under a parent category on the main menu
- how to filter posts by category without page load (ajax) in wordpress?
- How to increase the number posts that displays by a hierarchical taxonomy? (HTTP ERROR 500)
- How to distinguish pages created by woocommerce?
- How to create a button to filter posts (list) by meta value
- WordPress the_category(); only works with message-posts not with project posts, how do I specify project categories?
- How to display particular categorie’s post which associated to specific user?
- Display Specific Categories posts on the home page
- I want to show recent post according to category of the post in single.php?
- Limit amount of categories displayed in a post
- show only one category and filter by tag
- Code to display random ordered posts in Categories across pages [duplicate]
- AJAX load more posts not using correct category and repeating the same few posts
- Category and Posts in front page
- Allow registering user to select post category when signing up to blog
- How can I exclude only a specific sub category post from category display?
- how to print total number of posts filtered by category?
- Nothing appears using get_the_excerpt() in category.php
- Add category information beneath post?
- Custom plugin to get related posts by category
- Unable to uncheck a category in a post
- Next/Prev category post in same button
- Newly created category does not show category
- Show category post with excerpt text
- How to show different categories in different pages?
- How to set a link to custom archieve page
- Show a Category Specific Info Box
- Older entries link within a category page
- how to style an individual page in a category [duplicate]
- each user having category with their name and can add sub categories post in that category
- Can’t assign another category other than Uncategorized int he following front end post submission form?
- exclude certain categories from posts
- CSS – Change height of Storefront / WooCommerce product categories, but not products
- How to display my categories in a list (to a post, not the sidebar)?
- wordpress category Description not allowed tag
- How to sort posts alphabetically based on a specific parent category