FEATURE Introduction de plusieurs fonctions pour récupérer et organiser les termes parents/enfants

This commit is contained in:
Nonimart 2025-06-13 12:49:22 +02:00
parent 10668511d7
commit e87521f923

View File

@ -1,5 +1,66 @@
<?php
function get_the_parent_terms($taxonomy, $postID = null)
{
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
$terms = get_the_terms($postID, $taxonomy);
if (!$terms || empty($terms)) return [];
$parentTerms = array_filter($terms, function ($term) {
return $term->parent == 0;
});
return $parentTerms;
}
function get_the_children_terms($taxonomy, $postID)
{
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
$terms = get_the_terms($postID, $taxonomy);
if (!$terms || empty($terms)) return [];
$childrenTerms = array_filter($terms, function ($term) {
return $term->parent !== 0;
});
return $childrenTerms;
}
function get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm)
{
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
$childrenTerms = get_the_children_terms($taxonomy, $postID);
$filteredChildrenTerms = array_filter($childrenTerms, function ($term) use ($parentTerm) {
return $term->parent === $parentTerm->term_id;
});
return $filteredChildrenTerms;
}
function get_the_terms_organised_by_parent($taxonomy, $postID = null)
{
if (!$postID) return;
$terms = get_the_terms($postID, $taxonomy);
$sortedTermsByParent = [];
if (!$terms) return $sortedTermsByParent;
$parentTerms = get_the_parent_terms($taxonomy, $postID);
foreach ($parentTerms as $parentTerm) {
$sortedTermsByParent[] = $parentTerm;
$childrenTerms = get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm);
foreach ($childrenTerms as $childTerm) {
$sortedTermsByParent[] = $childTerm;
}
}
return $sortedTermsByParent;
}
function getAllBatimentsTermsByParents()
{
// Récupérer tous les termes de la taxonomie 'elementsbatiments' avec une hiérarchie