Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. function get_most_recent_categories($limit = 5) {
  2. // retrieve all categories
  3. $categories = get_categories();
  4. $recent_posts = array();
  5. foreach ($categories as $key=>$category) {
  6. // get latest post from $category
  7. $args = array(
  8. 'numberposts' => 1,
  9. 'category' => $category->term_id,
  10. );
  11. $post = get_posts($args)[0];
  12. // save category id & post date in an array
  13. $recent_posts[ $category->term_id ] = strtotime($post->post_date);
  14. }
  15.  
  16. // order by post date, preserve category_id
  17. arsort($recent_posts);
  18.  
  19. // get $limit most recent category ids
  20. $recent_categories = array_slice(array_keys($recent_posts), 0, $limit);
  21.  
  22. return $recent_categories;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement