Advertisement
Guest User

Untitled

a guest
Aug 26th, 2015
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2. /* Get all categories */
  3. $categories = get_categories();
  4. /* Create empty array */
  5. $categories_order = [];
  6. /* For each category */
  7. foreach($categories as $category) {
  8.   /* Modify WP query */
  9.   $args = array('posts_per_page' => 1, /* Max 1 post */
  10.                 'category__in' => array($category->term_id), /* In this specific category */
  11.                 'ignore_sticky_posts' => true ); /* No sticky posts */
  12.   /* Get all posts from categories with modifier */
  13.   $posts = get_posts($args);
  14.   /* If there are posts */
  15.   if ($posts) {
  16.     /* For each post */
  17.     foreach($posts as $post) {
  18.       /* Add to array key => value (category id => time published) */
  19.       $categories_order[$category->term_id] = get_post_time('YmdHis');
  20.     }
  21.   }
  22. }
  23. arsort($categories_order); /* Order new array by value */
  24. $categories_order = array_keys($categories_order); /* Remove array values */
  25. /* Function to compare $categories_order with $categories->term_id */
  26. function listcmp($a, $b) {
  27.   global $categories_order;
  28.   foreach($categories_order as $key => $value){
  29.       if($a->term_id==$value){
  30.           return 0;
  31.           break;
  32.         }
  33.       if($b->term_id==$value){
  34.           return 1;
  35.           break;
  36.         }
  37.     }
  38. }
  39. /* Create categories array */
  40. $categories = get_categories();
  41. /* Compare are rearrange $categories accordingly */
  42. usort($categories, "listcmp");
  43.  
  44.   foreach($categories as $category) {
  45.     /* Loop Here */
  46.   }
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement