Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Auto-assign category and add post to menu on publish
- add_action('transition_post_status', 'auto_categorize_and_add_to_menu', 10, 3);
- function auto_categorize_and_add_to_menu($new_status, $old_status, $post) {
- // Only run when a post transitions from draft to publish
- if ($new_status === 'publish' && $old_status === 'draft' && $post->post_type === 'post') {
- // Step 1: Auto-assign category based on keywords
- $title = strtolower($post->post_title);
- $content = strtolower($post->post_content);
- // Define keyword-to-category mapping (customize this)
- $category_mapping = [
- 'technology' => 'Technology', // Keyword => Category Name
- 'health' => 'Health',
- 'finance' => 'Finance',
- // Add more keywords and categories as needed
- ];
- $assigned_category = false;
- foreach ($category_mapping as $keyword => $category_name) {
- if (strpos($title, $keyword) !== false || strpos($content, $keyword) !== false) {
- // Get or create the category
- $category = get_term_by('name', $category_name, 'category');
- if (!$category) {
- $category_id = wp_insert_category([
- 'cat_name' => $category_name,
- 'category_description' => 'Auto-created category for ' . $category_name,
- 'category_nicename' => sanitize_title($category_name),
- ]);
- $category = get_term_by('id', $category_id, 'category');
- }
- // Assign the category to the post
- wp_set_post_categories($post->ID, [$category->term_id], true);
- $assigned_category = $category->term_id;
- break; // Assign only one category (remove to allow multiple)
- }
- }
- // Fallback: Assign a default category if no keywords match
- if (!$assigned_category) {
- $default_category = get_term_by('name', 'Uncategorized', 'category');
- if ($default_category) {
- wp_set_post_categories($post->ID, [$default_category->term_id], true);
- $assigned_category = $default_category->term_id;
- }
- }
- // Step 2: Add post to the navigation menu
- if ($assigned_category) {
- // Define the menu to update (replace 'primary-menu' with your menu's slug or ID)
- $menu_name = 'primary-menu';
- $menu = wp_get_nav_menu_object($menu_name);
- if ($menu) {
- // Get the category term
- $category = get_term($assigned_category, 'category');
- // Check if the category is already a menu item
- $menu_items = wp_get_nav_menu_items($menu->term_id);
- $parent_menu_item_id = 0;
- foreach ($menu_items as $item) {
- if ($item->object === 'category' && $item->object_id == $category->term_id) {
- $parent_menu_item_id = $item->ID;
- break;
- }
- }
- // If category isn't in the menu, add it
- if ($parent_menu_item_id === 0) {
- $parent_menu_item_id = wp_update_nav_menu_item($menu->term_id, 0, [
- 'menu-item-title' => $category->name,
- 'menu-item-object' => 'category',
- 'menu-item-object-id' => $category->term_id,
- 'menu-item-type' => 'taxonomy',
- 'menu-item-status' => 'publish',
- ]);
- }
- // Add the post as a sub-menu item under the category
- wp_update_nav_menu_item($menu->term_id, 0, [
- 'menu-item-title' => $post->post_title,
- 'menu-item-object' => 'post',
- 'menu-item-object-id' => $post->ID,
- 'menu-item-type' => 'post_type',
- 'menu-item-status' => 'publish',
- 'menu-item-parent-id' => $parent_menu_item_id,
- ]);
- }
- }
- }
- }
- // Optional: Limit menu size to prevent bloat (e.g., keep only the latest 10 posts per category)
- add_action('wp_update_nav_menu', 'limit_menu_items_per_category');
- function limit_menu_items_per_category($menu_id) {
- $menu = wp_get_nav_menu_object($menu_id);
- if (!$menu) {
- return;
- }
- $menu_items = wp_get_nav_menu_items($menu_id);
- $category_items = [];
- // Group menu items by parent category
- foreach ($menu_items as $item) {
- if ($item->menu_item_parent != 0 && $item->type === 'post_type') {
- $category_items[$item->menu_item_parent][] = $item;
- }
- }
- // Keep only the latest 10 posts per category
- foreach ($category_items as $parent_id => $items) {
- if (count($items) > 10) {
- usort($items, function ($a, $b) {
- return get_post_time('U', true, $b->object_id) - get_post_time('U', true, $a->object_id);
- });
- for ($i = 10; $i < count($items); $i++) {
- wp_delete_nav_menu_item($menu_id, $items[$i]->ID);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement