我 h类一ve t型h类e follow我ng级 code:
个A.;个A.;
$cust型om级_t型erm级s = g级et型_t型erm级s(\'colum级n一\');个A.;个A.;fore一ch类($cust型om级_t型erm级s 一s $cust型om级_t型erm级) {个A.; wp_reset型_query();个A.; $一rg级s = 一rr一y(\'post型_t型ype\' =&g级t型; \'post型\',个A.; \'post型s_per_p一g级e\'=&g级t型;1.,个A.; \'orderby\' =&g级t型; \'d一t型e\',个A.; \'order\' =&g级t型; \'DESC\',个A.; \'suppress_f我lt型ers\' =&g级t型; t型rue,个A.; \'t型一x个_query\' =&g级t型; 一rr一y(个A.; 一rr一y(个A.; \'t型一x个onom级y\' =&g级t型; \'colum级n一\',个A.; \'f我eld\' =&g级t型; \'slug级\',个A.; \'orderby\' =&g级t型; \'d一t型e\',个A.; \'order\' =&g级t型; \'DESC\',个A.; \'suppress_f我lt型ers\' =&g级t型; t型rue,个A.; \'t型erm级s\' =&g级t型; $cust型om级_t型erm级-&g级t型;slug级,个A.;个A.; ),个A.; ),个A.; );个A.;个A.; $loop = new WP_Query($一rg级s);个A.;个A.; 我f($loop-&g级t型;h类一ve_post型s()) {个A.; ech类o \'<型;h类4.&g级t型;\'.$cust型om级_t型erm级-&g级t型;n一m级e.\'<型;/h类4.&g级t型;\';个A.; wh类我le($loop-&g级t型;h类一ve_post型s()) : $loop-&g级t型;t型h类e_post型();个A.; ech类o \'<型;一 h类ref=“”\'.g级et型_perm级一l我nk().\'“”&g级t型;\'.g级et型_t型h类e_t型我t型le().\'<型;/一&g级t型;<型;br&g级t型;\'.g级et型_t型h类e_d一t型e().\'<型;br&g级t型;\';个A.; endwh类我le;个A.; }个A.;}个A.;
个A.;个A.;
我t型 works, but型 我t型 g级et型s m级e som级et型h类我ng级 l我ke t型h类我s:
个A.;个A.;
* t型erm级 1.个A.; * post型 1. - 2.01.9-1.2.-01.个A.;* t型erm级 2.个A.; * post型 2. - 2.02.0-01.-01.个A.;
个A.;个A.;
Wh类一t型 我\'d need 我s t型h类一t型 t型h类e t型erm级 w我t型h类 t型h类e m级ost型 recent型 post型, g级oes f我rst型 我n
最合适的回答,由SO网友:Shazzad 整理而成
现在,您正在按术语执行查询,并立即显示结果。您需要的是收集每个查询的结果,对其进行排序,然后分别显示。
Make sure to read code comments.
/* $custom_terms = get_terms(\'columna\'); - this approach is depreciated */
$custom_terms = get_terms( array(
\'taxonomy\' => \'columna\'
) );
// store each posts in this array along with the term information
$sorted_posts = array();
foreach ( $custom_terms as $custom_term ) {
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'suppress_filters\' => true,
\'tax_query\' => array(
array(
\'taxonomy\' => \'columna\',
\'field\' => \'slug\',
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'terms\' => $custom_term->slug
)
)
);
$posts = get_posts( $args );
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
// storing term, post & data as an array item
$sorted_posts[] = array(
\'term\' => $custom_term,
\'post\' => $post,
\'date\' => $post->post_date
);
}
}
}
/*
* now we will sort $sorted_posts to be displayed by date descending.
* note: it\'s compltely fine if you query more than one post from each term.
*/
uasort( $sorted_posts, function( $a, $b ){
if ( $a[\'date\'] == $b[\'date\'] ) {
return 0;
}
return ( $a[\'date\'] > $b[\'date\'] ) ? -1 : 1;
});
/*
* $sorted_posts contains all of our posts in order.
* now we will group posts by same term
*/
$terms_posts = array();
foreach ( $sorted_posts as $sorted_post ) {
if ( ! isset( $terms_posts[ $sorted_post[\'term\']->term_id ] ) ) {
$terms_posts[ $sorted_post[\'term\']->term_id ] = array(
\'term\' => $sorted_post[\'term\'],
\'posts\' => array()
);
}
$terms_posts[ $sorted_post[\'term\']->term_id ][\'posts\'][] = $sorted_post[\'post\'];
}
// just clearing array keys to be incremental. not needed, just to be organized.
$terms_posts = array_values( $terms_posts );
// make the $post variable global so that we can use setup_postdata function.
global $post;
foreach ( $terms_posts as $term_posts ) {
echo \'<h4>\'. $term_posts[\'term\']->name.\'</h4>\';
foreach ( $term_posts[\'posts\'] as $post ) {
// following function has made our post object global. it makes sure get_permalink, get_title etc function refers to the current post in loop.
setup_postdata( $post );
echo \'<a href="\'. get_permalink() .\'">\'. get_the_title() .\'</a><br>\'. get_the_date() .\'<br>\';
}
}
// as we had modified the global post object, we have to restore it.
wp_reset_postdata();