如何创建一个类别ID按最近发布文章的类别ID排序的数组

时间:2014-12-20 作者:user1255607

我找不到合适的解决办法。

我有一系列类别,例如:

$slidercategories_array = array(3,5,6,9,10,11,12,23,24,27); 
我需要像这样对数组重新排序,首先是最新帖子的类别,最后是最旧帖子的类别。

任何想法都会有帮助。

1 个回复
SO网友:bonger

You could try

$slidercategories_array = array(3,5,6,9,10,11,12,23,24,27);
$in = implode( \',\', array_map( \'absint\', $slidercategories_array ) );
global $wpdb;
$sql = $wpdb->prepare(
    \'SELECT tt.term_id, MAX(p.post_date) AS max_date FROM \' . $wpdb->term_taxonomy . \' AS tt \'
    . \' JOIN \' . $wpdb->term_relationships . \' AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id\'
    . \' LEFT JOIN \' . $wpdb->posts . \' AS p ON (p.ID = tr.object_id AND p.post_type = %s AND p.post_status = %s)\'
    . \' WHERE tt.term_id IN (\' . $in . \') AND tt.taxonomy = %s\'
    . \' GROUP BY tt.term_id\'
    . \' ORDER BY max_date DESC\'
    , \'post\', \'publish\', \'category\' );
$slidercategories_sorted = $wpdb->get_col( $sql );
结束

相关推荐

使用GET_CATEGORIES时突出显示当前类别

我成功地使用了helgatheviking在这篇文章中的回答:Show children of top level category only 请参见下面的代码:// get the category object for the current category $thisCat = get_category( get_query_var( \'cat\' ) ); // if not top-level, track it up the chain to find its