Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Get all categories */
- $categories = get_categories();
- /* Create empty array */
- $categories_order = [];
- /* For each category */
- foreach($categories as $category) {
- /* Modify WP query */
- $args = array('posts_per_page' => 1, /* Max 1 post */
- 'category__in' => array($category->term_id), /* In this specific category */
- 'ignore_sticky_posts' => true ); /* No sticky posts */
- /* Get all posts from categories with modifier */
- $posts = get_posts($args);
- /* If there are posts */
- if ($posts) {
- /* For each post */
- foreach($posts as $post) {
- /* Add to array key => value (category id => time published) */
- $categories_order[$category->term_id] = get_post_time('YmdHis');
- }
- }
- }
- arsort($categories_order); /* Order new array by value */
- $categories_order = array_keys($categories_order); /* Remove array values */
- /* Function to compare $categories_order with $categories->term_id */
- function listcmp($a, $b) {
- global $categories_order;
- foreach($categories_order as $key => $value){
- if($a->term_id==$value){
- return 0;
- break;
- }
- if($b->term_id==$value){
- return 1;
- break;
- }
- }
- }
- /* Create categories array */
- $categories = get_categories();
- /* Compare are rearrange $categories accordingly */
- usort($categories, "listcmp");
- foreach($categories as $category) {
- /* Loop Here */
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement