我想根据最新发布时间对类别进行排序

时间:2013-06-26 作者:Lalit Arora

例如,如果类别A有昨天创建的帖子1,类别B有今天创建的帖子2。因此,该类别将列在orderCategory B category A中

以前有人实施过吗?

2 个回复
最合适的回答,由SO网友:Ravinder Kumar 整理而成

此代码根据该类别中最近发布的帖子列出所有父类别。

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>\';
}

SO网友:MahdiY

试试看:

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\') );
获取类别订单依据(post ID | post date | post modified date)。没有任何循环和快速!

结束

相关推荐

按MENU_ORDER列出CPT,子项紧随父项之后

我注册了自定义帖子类型drawer 我的帖子是使用menu_order. 每个抽屉可以是另一个抽屉的子抽屉,在多个深度上(父>子>子的子>子的子……你知道了;))。到目前为止,我的代码如下所示:$args = array( \'numberposts\' => -1, \'orderby\' => \'menu_order\', \'order\' => \'DESC\',&#x