WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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 */
                'id' => $cat->term_id,
                'name' => $cat->name,
                'post_date' => $recent_posts[0]['post_date']

    /* 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>
    echo '</ul>';
share|improve this answer
Seems legit. Tested this and it works. Go Ravs! – jounileander Jun 26 '13 at 8:24
@Ravs Nice. But do you recommend using this functionality. As, I think it will consume lots of bandwidth. – Lalit Arora Jun 27 '13 at 9:43
I don't have much information about your case.Their are lots of tricks to do that but all depend on your case. – Ravinder Kumar Jun 27 '13 at 9:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.