Merge pull request #2 from PxaMMaxP/nested-meta-sorting

Enhanced Sorting by Nested Meta Values in Pages
This commit is contained in:
Max P 2023-12-01 14:28:39 +01:00 committed by GitHub
commit 78fdffd699
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1877,11 +1877,22 @@ class Pico
if ($orderBy === 'meta') {
// sort by arbitrary meta value
$orderByMeta = $this->getConfig('pages_order_by_meta');
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMeta) {
$aSortValue = isset($a['meta'][$orderByMeta]) ? $a['meta'][$orderByMeta] : null;
// Split the configuration value into an array of keys to allow sorting by nested meta values.
$orderByMetaKeys = explode('.', $orderByMeta);
uasort($this->pages, function ($a, $b) use ($alphaSortClosure, $order, $orderByMetaKeys) {
$aMeta = $a['meta'];
$bMeta = $b['meta'];
// Iterate through the meta keys to drill down into nested arrays for the final sort value.
foreach ($orderByMetaKeys as $key) {
$aMeta = isset($aMeta[$key]) ? $aMeta[$key] : null;
$bMeta = isset($bMeta[$key]) ? $bMeta[$key] : null;
}
$aSortValue = $aMeta;
$aSortValueNull = ($aSortValue === null);
$bSortValue = isset($b['meta'][$orderByMeta]) ? $b['meta'][$orderByMeta] : null;
$bSortValue = $bMeta;
$bSortValueNull = ($bSortValue === null);
$cmp = 0;