Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting menu by Published Date
#1
Got another problem, ive created a menu function from get_navigation() to return any page that is not set as belong to the menu, however i cant get them to sort in the correct order.

Does anyone know what code or variable i would need to change in the get_navigation() function to facilitate sorting be the date the page was published?

Ive messed around with the code to the limit of my ability now with no joy.

Thanks! Big Grin
Reply
#2
Ok i managed to partially fix this using pubdate, but the pudate data is stored in this format in this way <pubDate>Thu, 01 Oct 2009 17:26:15 +0100</pubDate> so it sorts by the first data in that line which is the day rather than the date.

Does anyone know how to refine this so it searches by date then time?

this is the line of code in question
$pagesSorted = subval_sort($pagesArray,'pubdate');

Thanks
Reply
#3
the way I did it was convert the pubDate to Unix format time and then do a normal sort using strtotime.

Bear in mind that this is not really the published date but is updated on every update of a page.
SO if you edit you pages in the future the sort order will be different.

Mike.
My Github Repos: Github
Website: DigiMute
Reply
#4
Mike, thanks ill look into that possibly sounds out of my current scope of php coding, but ill look into it tomorow. Yeh did notice about the date updating when a page is changed, ill edit any that give me problems manually Big Grin
Reply
#5
This is the way I did it for sorting Blog entries on my site.


my guess is you've taken the original menu code and changed it so your extracting the pubdate like so

Code:
$pagesArray[$count]['pubdate']=$data->pubDate;

change this to the following to convert the date/time to unixtime format, this make it easier
to sort on.

Code:
$pagesArray[$count]['pubdate']=strtotime($data->pubDate);

change the sort line to

Code:
$pagesSorted = sort2d($pagesArray,'pubdate','desc');

add the following function to your code somewhere

Code:
function sort2d ($array, $index, $order='asc', $natsort=FALSE, $case_sensitive=FALSE)  
    {
        if(is_array($array) && count($array)>0)  
        {
           foreach(array_keys($array) as $key)  
               $temp[$key]=$array[$key][$index];
               if(!$natsort)  
                   ($order=='asc')? asort($temp) : arsort($temp);
              else  
              {
                 ($case_sensitive)? natsort($temp) : natcasesort($temp);
                 if($order!='asc')  
                     $temp=array_reverse($temp,TRUE);
           }
           foreach(array_keys($temp) as $key)  
               (is_numeric($key))? $sorted[]=$array[$key] : $sorted[$key]=$array[$key];
           return $sorted;
      }
      return $array;
    }

use date() function to convert the unixtime format back into human readable.

Code:
echo date('M d Y, g:i:s',$page['pubdate']);

hope it helps....

Mike.
My Github Repos: Github
Website: DigiMute
Reply
#6
it does help very much so, my sincere gratitude. Yep i took the origional menu function just to return anything not marked as list on the menu to manage my blog posts. Big Grin

Ok yes its sorting correctly and i added the echo date('M d Y, g:iConfused',$page['pubdate']); to my template.php theme file

and i added the two lines of code to the edited navigation function i copied and also added your sort2d function in the same theme_functions.php

but its returning the standard unix start date of 1970.

Will investigate more tomorow eyes are hurting now hehe Big Grin

Edit: scrap that just replaced the theme function back to how it was and its all good now thanks again!
Reply




Users browsing this thread: 1 Guest(s)