Advertisement
vtxyzzy

Sort posts on max date in Custom Field

Jul 1st, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.38 KB | None | 0 0
  1.    // List custom posts in ascending order of the maximum date
  2.    // contained in a list of dates in a custom field.  The dates
  3.    // are in d-m-yyyy format.
  4.    // Example meta_value: 1-10-2011, 17-2-2013, 25-6-2014
  5.  
  6.    $post_type = 'parks';
  7.    $meta_key = 'dates_of_leaving';
  8.  
  9.    // Get post IDs and dates_of_leaving
  10.    $sql = "
  11.      SELECT p.ID, pm.meta_value AS dates
  12.      FROM $wpdb->posts p
  13.      JOIN $wpdb->postmeta pm ON (p.ID = pm.post_id)
  14.      WHERE p.post_type = '$post_type'
  15.         AND pm.meta_key = '$meta_key'
  16.         AND p.post_status = 'publish'
  17.      ORDER BY p.ID ASC
  18.   ";
  19.    $rows = $wpdb->get_results($sql);
  20.  
  21.    // Get the max date for each ID. Save the ID and date for sorting
  22.    $for_sorting = array();
  23.    $current_id = 0;
  24.    foreach ($rows as $row) {
  25.       $newdates = array();
  26.       $olddates = explode(',',$row->dates);
  27.       // Reformat and save the dates
  28.       foreach ($olddates as $olddate) {
  29.          if (preg_match('/(\d+)-(\d+)-(\d{4})/', $olddate, $matches ) ) {
  30.             $newdates[] = $matches[3] .
  31.             str_pad($matches[2], 2, '0', STR_PAD_LEFT) .
  32.             str_pad($matches[1], 2, '0', STR_PAD_LEFT);
  33.          }
  34.       }
  35.       rsort($newdates);  // Sort the dates high to low
  36.       //print_r('<pre>DATES:');print_r($newdates);print_r('</p>');
  37.       if ($newdates) {
  38.          $for_sorting[] = array('ID' => $row->ID, 'date' => $newdates[0]);
  39.       }
  40.    }
  41.    //print_r('<pre>FORSORTING:');print_r($for_sorting);print_r('</p>');
  42.  
  43.    // Now, sort the array with ID and date
  44.    usort($for_sorting, function($a, $b) {
  45.         return $a['date'] - $b['date'];});
  46.    //print_r('<pre>SORTED:');print_r($for_sorting);print_r('</p>');
  47.  
  48.    // Get just the IDs, in order
  49.    $id_array = array();
  50.    foreach ($for_sorting as $sorted) {
  51.       $id_array[] = $sorted['ID'];
  52.    }
  53.    // Show posts in order of post__in array
  54.    $args = array(
  55.       'post_type' => $post_type,
  56.       'post__in' => $id_array,
  57.       'ignore_sticky_posts' => 1,  // Stickies will mess this up
  58.    );
  59.    query_posts($args);
  60.    if (have_posts()) :
  61.       usort($wp_query->posts, function($a, $b) use ($id_array) {
  62.          return array_search($a->ID, $id_array) - array_search($b->ID, $id_array);
  63.       });
  64.       while (have_posts()) :
  65.          // Put your own loop code here
  66.          the_post();
  67.          echo "<p>ID: $post->ID</p>";
  68.       endwhile;
  69.    endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement