我有一个以slug顺序显示的术语列表,但我需要按最近的帖子进行排序,因此它将根据最新条目的术语而变化。。。有没有办法做到这一点?我尝试了这里提出的解决方案:Retrieve taxonomy terms in order of their post's date?
但它显示的是术语的创建日期,而不是最近帖子的日期。
我的代码:
function wpse147412_order_terms_by_post_date( $pieces, $taxonomies, $args ) {
global $wpdb;
if ( \'post_date\' !== $args[\'orderby\'] ) {
return $pieces;
}
$args = wp_parse_args( $args, array( \'post_types\' => \'episode\' ) );
$pieces[\'fields\'] = \'DISTINCT \' . $pieces[\'fields\'];
$pieces[\'join\'] .= " JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
$pieces[\'join\'] .= " JOIN $wpdb->posts AS p ON p.ID = tr.object_id";
$pieces[\'where\'] .= " AND p.post_type IN (\'" . implode( "\', \'", (array) $args[\'post_types\'] ) . "\')";
$pieces[\'orderby\'] = \'ORDER BY p.post_date\';
return $pieces;
}
add_filter( \'terms_clauses\', \'wpse147412_order_terms_by_post_date\', 10, 3 );
放在上面的函数中,然后在页面中我需要这些术语:<?php
$tax_terms = get_terms( \'series\',
array( \'post_types\' => \'episode\',
\'orderby\' => \'post_date\',
\'order\' => \'DESC\' )
);
if( is_array( $tax_terms ) && count( $tax_terms ) > 0 ):
foreach ($tax_terms as $tax_term):
?>
<div class="series-button text-center">
<a href="<?php echo esc_attr(get_term_link($tax_term, $taxonomy));?>">
<figure class="align-items-center d-flex" style="background-image: url(\'<?php echo z_taxonomy_image_url($tax_term->term_id, \'series-img\'); ?>\');">
<h3 class="text-center uppercase"><?php echo $tax_term->name; ?></h3>
</figure>
</a>
</div>
<?php
endforeach;
endif;
?>