Auto Taxonomy Assignment
I use this code snippet to automatically assign posts to a taxonomy based on the first letter of the post. See this video for more details.
//------------------------------------------------------------------------------ //This function auto adds the glossary entry to the appropriate category function auto_assign_glossary_section($post_id, $post, $update) { // Only proceed for published glossary posts if (get_post_status($post_id) !== 'publish' || get_post_type($post_id) !== 'glossary') { return; } // Check if the taxonomy exists if (!taxonomy_exists('glossary-sections')) { return; } // Get the post title $post_title = get_the_title($post_id); // Get the first non-whitespace character $first_char = trim($post_title)[0] ?? ''; // Determine the term name $term_name = ctype_alpha($first_char) ? strtoupper($first_char) : '#'; // Try to get the term $term = get_term_by('name', $term_name, 'glossary-sections'); if ($term) { // If the term exists, assign it to the post wp_set_object_terms($post_id, $term->term_id, 'glossary-sections'); } else { // If the term doesn't exist, create it and assign it $new_term = wp_insert_term($term_name, 'glossary-sections'); if (!is_wp_error($new_term)) { wp_set_object_terms($post_id, $new_term['term_id'], 'glossary-sections'); } } } add_action('wp_insert_post', 'auto_assign_glossary_section', 10, 3);