diff --git a/includes/utilities.php b/includes/utilities.php index 5a5b0eb..b9b8547 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -66,6 +66,11 @@ function getMainThematique($thematique) return get_term($thematique->parent, 'thematiques'); } } +// DOES THE SAME THING AS ABOVE BUT RENAMED +function getPostMainThematique($post) +{ + return getMainThematique(get_the_terms($post, "thematiques")[0]); +} // TO GET ARCHIVE RELATED PAGES BY TEMPLATE function get_page_by_template($template = '') @@ -100,3 +105,48 @@ function show_post_type_label_name($post_type) return null; } } + + + + + +/* --------- +SORT BY THEMATIQUE PRIORITY +-----------------*/ + +// Fonction de comparaison pour trier par 'thematique_priority' utilisé par la fonction supérieur +function compareThematiquePriority($postA, $postB) +{ + $priorityA = $postA->thematique_priority ?? 0; + $priorityB = $postB->thematique_priority ?? 0; + + if ($priorityA == $priorityB) { + if ($postA->post_date > $postB->post_date) { + return -1; + } else { + return 1; + } + } + if ($priorityA < $priorityB) { + return 1; + } + if ($priorityA > $priorityB) { + return -1; + } +} + + +function sort_posts_per_thematiques_priority($postsQuery) +{ + + foreach ($postsQuery->posts as $key => $post) { + $thematique = getPostMainThematique($post); + $post->thematique_priority = get_field('thematique_order', $thematique) ?? 0; + } + + + + // Tri du tableau en utilisant la fonction de comparaison + usort($postsQuery->posts, 'compareThematiquePriority'); + return $postsQuery; +}