Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

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 */
        $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>';
}
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. –  Ravs Jun 27 '13 at 9:50

Your Answer

 
discard

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.