3

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?

2 Answers 2

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>';
}
3
  • Seems legit. Tested this and it works. Go Ravs! Commented Jun 26, 2013 at 8:24
  • @Ravs Nice. But do you recommend using this functionality. As, I think it will consume lots of bandwidth. Commented Jun 27, 2013 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. Commented Jun 27, 2013 at 9:50
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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