列出分类术语以及按发帖日期排序的最新帖子

时间:2020-04-11 作者:Joaquín Navarro

我 h类一ve t型h类e follow我ng级 code:

&#x个A.;&#x个A.;
$cust型om级_t型erm级s = g级et型_t型erm级s(\'colum级n一\');&#x个A.;&#x个A.;fore一ch类($cust型om级_t型erm级s 一s $cust型om级_t型erm级) {&#x个A.;    wp_reset型_query();&#x个A.;    $一rg级s = 一rr一y(\'post型_t型ype\' =&g级t型; \'post型\',&#x个A.;        \'post型s_per_p一g级e\'=&g级t型;1.,&#x个A.;        \'orderby\' =&g级t型; \'d一t型e\',&#x个A.;        \'order\'   =&g级t型; \'DESC\',&#x个A.;        \'suppress_f我lt型ers\' =&g级t型; t型rue,&#x个A.;        \'t型一x个_query\' =&g级t型; 一rr一y(&#x个A.;            一rr一y(&#x个A.;                \'t型一x个onom级y\' =&g级t型; \'colum级n一\',&#x个A.;                \'f我eld\' =&g级t型; \'slug级\',&#x个A.;                \'orderby\' =&g级t型; \'d一t型e\',&#x个A.;                \'order\'   =&g级t型; \'DESC\',&#x个A.;                \'suppress_f我lt型ers\' =&g级t型; t型rue,&#x个A.;                \'t型erm级s\' =&g级t型; $cust型om级_t型erm级-&g级t型;slug级,&#x个A.;&#x个A.;            ),&#x个A.;        ),&#x个A.;     );&#x个A.;&#x个A.;     $loop = new WP_Query($一rg级s);&#x个A.;&#x个A.;     我f($loop-&g级t型;h类一ve_post型s()) {&#x个A.;        ech类o \'<型;h类4.&g级t型;\'.$cust型om级_t型erm级-&g级t型;n一m级e.\'<型;/h类4.&g级t型;\';&#x个A.;        wh类我le($loop-&g级t型;h类一ve_post型s()) : $loop-&g级t型;t型h类e_post型();&#x个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型;\';&#x个A.;        endwh类我le;&#x个A.;     }&#x个A.;}&#x个A.;
&#x个A.;&#x个A.;

我t型 works, but型 我t型 g级et型s m级e som级et型h类我ng级 l我ke t型h类我s:

&#x个A.;&#x个A.;
* t型erm级 1.&#x个A.; * post型 1. - 2.01.9-1.2.-01.&#x个A.;* t型erm级 2.&#x个A.; * post型 2. - 2.02.0-01.-01.&#x个A.;
&#x个A.;&#x个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

1 个回复
最合适的回答,由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();

相关推荐

我相信类别导致WP_Query Order By不能正常工作

我问了一个问题,应该在哪里按字段外部链接排序,然后按价格字段排序。问题似乎是有些帖子有多个类别,似乎无法理解为什么它不按外部链接恶魔的顺序排序,然后按价格字段排序。 $query = new WP_Query( array( \'post_type\' => post, \'posts_per_page\' => -1, \'meta_query\' =>