From 5624335a792499dd35794d2fa0b1d3812e6dac18 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Mon, 23 Mar 2026 16:46:14 +0100 Subject: [PATCH] FEATURE Enhancing get_authors_linked_to_posts to integrate main authors and sort authors list by name --- includes/utilities.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/includes/utilities.php b/includes/utilities.php index c28e8ad..fc4d1fa 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -379,26 +379,38 @@ function get_authors_linked_to_posts(array $post_ids) return array(); } $author_ids = array(); + foreach ($post_ids as $post_id) { $post_authors = get_field('authors', $post_id); - if (empty($post_authors)) continue; - foreach ($post_authors as $author) { - $author_id = is_object($author) ? $author->ID : (int) $author; - if ($author_id) { - $author_ids[$author_id] = $author_id; // set pour éviter les doublons + $has_main_author = get_field('has_main_author', $post_id); + $post_main_author = get_field('main_author', $post_id); + + if (!empty($post_authors)) { + foreach ($post_authors as $author) { + $author_id = is_object($author) ? $author->ID : (int) $author; + if ($author_id) { + $author_ids[$author_id] = $author_id; // set pour éviter les doublons + } } } + + + if ($has_main_author && is_object($post_main_author) && isset($post_main_author->ID)) { + $author_ids[$post_main_author->ID] = $post_main_author->ID; + } } if (empty($author_ids)) { return array(); } + return get_posts(array( 'post_type' => 'auteurs', 'post__in' => array_values($author_ids), 'posts_per_page' => -1, - 'orderby' => 'title', 'order' => 'ASC', + 'meta_key' => 'last_name', + 'orderby' => 'meta_value', )); }