if (!function_exists('dk_auto_assign_author')) { function dk_auto_assign_author($post_id, $post, $update) { if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) return; if ($post->post_status !== 'publish') return; if (!in_array($post->post_type, array('post', 'page'), true)) return; if (get_post_meta($post_id, '_dk_author_routed', true)) return; $term_to_duo = array( 28 => 'adhd_mental', // ADHD & Focus 29 => 'adhd_mental', // Mental Health 30 => 'parenting_single',// Parenting Tips 33 => 'parenting_single',// Single-Parent Life 31 => 'tools_books', // Tools & Apps 32 => 'tools_books', // Books & Guides ); $duo_users = array( 'adhd_mental' => array(4, 5), // Marcus Webb (M), Sofia Moreau (F) 'parenting_single' => array(9, 6), // Diego Rivera (M), Nour El-Rashidi (F) 'tools_books' => array(7, 8), // Tyler Brooks (M), Mei Lin Foster (F) ); $term_ids = wp_get_object_terms($post_id, 'category', array('fields' => 'ids')); if (is_wp_error($term_ids) || empty($term_ids)) return; $duo_key = null; foreach ($term_ids as $tid) { if (isset($term_to_duo[$tid])) { $duo_key = $term_to_duo[$tid]; break; } } if (!$duo_key) return; $alt = get_option('dk_author_alternation', array()); $last = isset($alt[$duo_key]) ? $alt[$duo_key] : -1; $next = ($last === -1) ? 0 : 1 - $last; $author_id = $duo_users[$duo_key][$next]; global $wpdb; $wpdb->update($wpdb->posts, array('post_author' => $author_id), array('ID' => $post_id)); clean_post_cache($post_id); update_post_meta($post_id, '_dk_author_routed', 1); update_post_meta($post_id, '_dk_author_duo', $duo_key); $alt[$duo_key] = $next; update_option('dk_author_alternation', $alt, false); } } if (!has_action('save_post', 'dk_auto_assign_author')) { add_action('save_post', 'dk_auto_assign_author', 20, 3); }