获取按发布日期排序的条款

时间:2019-04-05 作者:mistertaylor

我想要达到的目标:获得n 自定义分类法中的术语,按每个术语中的最新帖子排序。这将用于显示最新m 每个学期的职位。

有没有比获取所有术语的数组并循环查找每个术语中的最新帖子更好的方法?这种方法感觉不是很有效或可扩展。

Update

这是我目前为止的工作代码-这可以改进吗?

$taxonomy = \'industry\'; //taxonomy name
$terms_to_display = 5; //number of terms to display
$posts_per_term = 4; // number of posts per term to display
$terms = get_terms( array(\'taxonomy\' => $taxonomy, \'hide_empty\' => true, \'fields\' => \'ids\' ) );
$news_items = [];

foreach ( $terms as $term ) {

    $args = array (
        \'post_type\'         => \'news\',
        \'posts_per_page\'    => $posts_per_term,
        \'orderby\'           => \'date\',
        \'order\'             => \'DESC\',
        \'tax_query\' => array (
            array (
                \'taxonomy\' => $taxonomy,
                \'field\'    => \'id\',
                \'terms\'    => array($term),
            ),
        ),
    );

    $news = new WP_Query( $args );

    if ( $news->post_count == $posts_per_term ) { // ignore terms with not enough posts
        $news_items[$term] = get_the_date("U", $news->posts[0]->ID); //get date of newest post in this term
    }

    wp_reset_query();

}

arsort ( $news_items ); //sort descending, keeping keys

$term_ids = array_keys ( array_slice($news_items, 0, $terms_to_display, true) ); //take \'n\' newest and return an array of keys (term ids)

foreach ( $term_ids as $term_id ) {

    $term = get_term ( $term_id, $taxonomy );

    echo "<h2>" . $term->name . "</h2>";

    $args = array (
        \'post_type\'         => \'news\',
        \'posts_per_page\'    => $posts_per_term,
        \'orderby\'           => \'date\',
        \'order\'             => \'DESC\',
        \'tax_query\'         => array (
                array (
                    \'taxonomy\' => $taxonomy,
                    \'field\'    => \'id\',
                    \'terms\'    => array($term_id),
                ),
        ),
    );

    $news = new WP_Query( $args );

    if ( $news->have_posts() ) {    

        echo "<ul>";

        while ( $news->have_posts() ) {
            $news->the_post();

                echo "<li>" . get_the_title() . "</li>";

        }

        echo "</ul>";
    }

    wp_reset_query();

}

1 个回复
SO网友:djboris

基本上,您现在正在做的是:

获取所有术语industry 分类法对每个术语进行WP_Query 要获取最新帖子,请遍历它们以对术语进行排序,使用术语ID创建一个新数组对新数组进行排序和切片,对于术语ID的新数组中的每个元素,请再次执行WP_Query 获取最新的帖子,并反复浏览这些帖子WP_Queryies。让我们试着降低这个数字。

我们可以这样做:

获取所有术语industry 分类法对每个术语进行WP_Query 要获取最新的帖子,请遍历它们以对术语进行排序,创建一个新数组,新数组将键入U 格式,并包含术语对象和带有post对象的数组,因此我们不必再次查询它们以下是我的建议代码:

$taxonomy = \'industry\'; //taxonomy name
$terms_to_display = 5; //number of terms to display
$posts_per_term = 4; // number of posts per term to display
$terms = get_terms( array(\'taxonomy\' => $taxonomy, \'hide_empty\' => true ) );
$news_items = [];

foreach ( $terms as $term ) {

    $args = array (
        \'post_type\'         => \'news\',
        \'posts_per_page\'    => $posts_per_term,
        \'orderby\'           => \'date\',
        \'order\'             => \'DESC\',
        \'tax_query\' => array (
            array (
                \'taxonomy\' => $taxonomy,
                \'field\'    => \'id\',
                \'terms\'    => array( $term->term_id ),
            ),
        ),
    );

    $news_query = new WP_Query( $args );

    // ignore terms with not enough posts
    if ( $news_query->post_count < $posts_per_term ) {
        continue;
    }

    $news_posts = $news_query->get_posts();

    // get date of newest post in this term
    $newest_post_date = get_the_date( "U", $news_posts[0]->ID );

    $news_items[$newest_post_date] = array(
        \'term\' => $term,
        \'news\' => $news_posts,
    );
}

krsort( $news_items ); // sort descending by keys

$news_items = array_slice( $news_items, 0, $terms_to_display );

wp_reset_query();
我还做了一些更改:
1。$terms 现在是WP_Term 对象,而不仅仅是ID。制作$news_query->post_count < $posts_per_term 检查更具可读性

以及如何使用:

foreach ( $news_items as $item ) {

    echo "<h2>" . $item[\'term\']->name . "</h2>";

    echo "<ul>";

    foreach ( $item[\'news\'] as $news_post ) {
        setup_postdata( $news_post );

        echo "<li>" . get_the_title() . "</li>";
    }

    echo "</ul>";
}

wp_reset_query();
这样,我们减少了WP_Queryies到industry 并使生成的数组更加有用。