From b9ae1822f973260972e4b482bb644cf84876a2a4 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Fri, 23 May 2025 18:07:15 +0200 Subject: [PATCH] ADD Implement dynamic article linking and unlinking for 'revues' in revue.php to manage related articles effectively. --- includes/revue.php | 67 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 includes/revue.php diff --git a/includes/revue.php b/includes/revue.php new file mode 100644 index 0000000..50b6b8e --- /dev/null +++ b/includes/revue.php @@ -0,0 +1,67 @@ + 'articles', + 'meta_query' => array( + array( + 'key' => 'related_revue', + 'value' => $current_revue_ID, + 'compare' => '=', + ), + ), + )); + + $articles_related_to_revue_posts = $articles_related_to_revue->posts; + + + $articles_related_to_revue_ids = array_map(function ($post) { + return $post->ID; + }, $articles_related_to_revue_posts); + + $articles_ids = array_map(function ($article) { + return $article->ID; + }, $articles); + + // GET THE ARTICLES THAT ARE NOT HANDLED BY THE PARENT REVUE IN THE ARTICLE FIELD + $unmapped_articles_ids = array_diff($articles_related_to_revue_ids, $articles_ids); + + + // EMPTY THE ARTICLE FIELD BECAUSE NOT USED BY THE PARENT REVUE + + foreach ($unmapped_articles_ids as $article_id) { + update_field('related_revue', null, $article_id); + } +} + +function dynamiques_revue_link_mapped_articles($current_revue_ID) +{ + $articles = get_field('articles', $current_revue_ID); + + foreach ($articles as $article) { + $article_id = $article->ID; + $related_revue = get_field('related_revue', $article_id); + update_field('related_revue', $current_revue_ID, $article_id); + } +}