Selaa lähdekoodia

Added functionality for sorting pages by nested meta values.

Max P 1 vuosi sitten
vanhempi
commit
3cf1a0aa4c
1 muutettua tiedostoa jossa 14 lisäystä ja 3 poistoa
  1. 14 3
      lib/Pico.php

+ 14 - 3
lib/Pico.php

@@ -1830,11 +1830,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;