I want to order categories based upon the latest post time

For example if category A has post 1 which is created yesterday and category B has post 2 which is created today.
So, the category will be listed in order
Category B
Category A

Have anyone implemented this before?

Related posts

2 comments

  1. This code list all parent categories on the basis of recent publish post in that category.

    function ravs_cat_list(){
    
        /* all parent categories*/
        $args = array(
                'parent' => 0
            );
        $cats = get_categories( $args );
    
        /* get recent post from each category */
        foreach( $cats as $cat ):
            $args = array(
                    'numberposts' => 1,
                    'category' => $cat->term_id
                );
            $recent_posts = wp_get_recent_posts( $args );
            /* category list */
            $cat_list[]=array(
                    'id' => $cat->term_id,
                    'name' => $cat->name,
                    'post_date' => $recent_posts[0]['post_date']
                );
        endforeach;
    
        /* sort $cat_list on basis of resent publish post */
        function sortFunction( $a, $b ){
            return strtotime($a["post_date"]) - strtotime($b["post_date"]) > 1 ? -1 : 1;
        }
        usort($cat_list, "sortFunction");
    
        /* print list of sorted categories */
        echo'<ul class="cat-list">';
        foreach ($cat_list as $cat):
        ?>
        <li><?php print_r($cat['name']); ?></li>
        <?php
        endforeach;
        echo '</ul>';
    }
    
  2. Try it:

    function get_sorted_categories( $order_by = 'id' ){
        global $wpdb;
    
        $category = get_categories();
    
        $order = [
            'id' => 'post.ID',
            'date' => 'post.post_date',
            'modified' => 'post.post_modified',
        ];
    
        $order_by = $order[ $order_by ];
    
        $q = $wpdb->get_results("SELECT tax.term_id FROM `{$wpdb->prefix}term_taxonomy` tax
        INNER JOIN `{$wpdb->prefix}term_relationships` rel ON rel.term_taxonomy_id = tax.term_id
        INNER JOIN `{$wpdb->prefix}posts` post ON rel.object_id = post.ID WHERE tax.taxonomy = 'category' AND post.post_type = 'post' AND post.post_status = 'publish' ORDER BY {$order_by} DESC");
    
        $sort = array_flip( array_unique( wp_list_pluck( $q, 'term_id' ) ) );
    
        usort( $category, function( $a, $b ) use ( $sort, $category ) {
            if( isset( $sort[ $a->term_id ], $sort[ $b->term_id ] ) && $sort[ $a->term_id ] != $sort[ $b->term_id ] )
                $res = ($sort[ $a->term_id ] > $sort[ $b->term_id ]) ? 1 : -1;
            else if( !isset( $sort[ $a->term_id ] ) && isset( $sort[ $b->term_id ] ) )
                $res = 1;
            else if( isset( $sort[ $a->term_id ] ) && !isset( $sort[ $b->term_id ] ) )
                $res = -1;
            else
                $res = 0;
    
            return $res;
        } );
    
        return $category;
    }
    
    print_r( get_sorted_categories() );
    print_r( get_sorted_categories('date') );
    print_r( get_sorted_categories('modified') );
    

    Get categories order by (post ID | post date | post modified date). Without any loop and fast!

Comments are closed.