FEATURE add get_authors_linked_to_posts function to retrieve authors associated with given posts via ACF field for enhanced content management
This commit is contained in:
parent
5ebdcf0831
commit
f45c1d0b40
|
|
@ -361,3 +361,42 @@ function get_carhop_members_by_comity()
|
|||
restore_current_blog();
|
||||
return $members_organised_by_comity;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Retourne les auteurs (posts du CPT "auteurs") réellement liés
|
||||
* aux posts donnés via le champ ACF "authors".
|
||||
*
|
||||
* @param array $post_ids Liste d'IDs de posts
|
||||
* @param string $acf_field_name Nom du champ ACF (par défaut "authors")
|
||||
* @return array|WP_Post[] Liste de posts "auteurs"
|
||||
*/
|
||||
function get_authors_linked_to_posts(array $post_ids)
|
||||
{
|
||||
if (empty($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
|
||||
}
|
||||
}
|
||||
}
|
||||
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',
|
||||
));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user