Wordpress 按最后一次发布获取_类别订单

Wordpress 按最后一次发布获取_类别订单,wordpress,Wordpress,在Wordpress中,按最后一篇文章订购的最佳和最短的方式是什么 这意味着最近发表的文章的类别应该首先出现,它是否可用?我认为最简单的方法是循环浏览按发布日期排序的所有文章,然后将类别保存到一个数组中。然后可以循环浏览类别并显示它们。他们会井然有序的 大概是这样的: <?php // Initiate array $cats = array(); // Query arguments $args = array( 'post_type' => 'post', '

在Wordpress中,按最后一篇文章订购的最佳和最短的方式是什么


这意味着最近发表的文章的类别应该首先出现,它是否可用?

我认为最简单的方法是循环浏览按发布日期排序的所有文章,然后将类别保存到一个数组中。然后可以循环浏览类别并显示它们。他们会井然有序的

大概是这样的:

<?php

// Initiate array
$cats = array();

// Query arguments
$args = array(
    'post_type' => 'post',
    'posts_per_page' => -1,
    'orderby' = 'post_date'.
    'order' => 'DESC'
);

// The query
$query = new WP_Query($args);

// The loop
if($query->have_posts()) {
    while($query->have_posts()) {
        $query->the_post();

        // Get the term object
        $term = get_the_category();

        // Make sure the term doesn't already exist in the array
        if(!array_key_exists($term[0]->ID, $cats)) {
            // Add the terms to the array
            $cats[$term[0]->ID] = $term;
        }
    }
}

foreach($cats as $cid => $cat) {
    // Loop through the categories here
}
?>
当然,正如上面的评论中提到的,您也可以用另一种方法,首先循环类别,然后对数组进行排序。这样做可以:

<?php

// Initiate array
$cats = get_categories();
$recent_cats = array();

foreach($cats as $k => $cat) {
    // Query arguments
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 1,
        'orderby' = 'post_date'.
        'order' => 'DESC',
        'cat' => $cat->term_id
    );

    // The query
    $query = new WP_Query($args);

    // The loop
    if($query->have_posts()) {
        while($query->have_posts()) {
            $query->the_post();
            $date_str = strtotime(the_date());
            if(!array_key_exists($date_str, $recent_cats)) {
                $recent_cats[$date_str] = $cat->name;
            }
        }
    }
}

krsort($recent_cats);

// Loop through $recent_cats here

?>
试试看:

function get_sorted_categories( $order_by = 'id', $args = array() ){
    global $wpdb;

    $category = get_categories( $args );

    $order = [
        'id' => 'post.ID',
        'date' => 'post.post_date',
        'modified' => 'post.post_modified',
    ];

    $order_by = $order[ $order_by ];

    $q = $wpdb->get_results("SELECT tax.term_id FROM `{$wpdb->prefix}term_taxonomy` tax
    INNER JOIN `{$wpdb->prefix}term_relationships` rel ON rel.term_taxonomy_id = tax.term_id
    INNER JOIN `{$wpdb->prefix}posts` post ON rel.object_id = post.ID WHERE tax.taxonomy = 'category' AND post.post_type = 'post' AND post.post_status = 'publish' ORDER BY {$order_by} DESC");

    $sort = array_flip( array_unique( wp_list_pluck( $q, 'term_id' ) ) );

    usort( $category, function( $a, $b ) use ( $sort, $category ) {
        if( isset( $sort[ $a->term_id ], $sort[ $b->term_id ] ) && $sort[ $a->term_id ] != $sort[ $b->term_id ] )
            $res = ($sort[ $a->term_id ] > $sort[ $b->term_id ]) ? 1 : -1;
        else if( !isset( $sort[ $a->term_id ] ) && isset( $sort[ $b->term_id ] ) )
            $res = 1;
        else if( isset( $sort[ $a->term_id ] ) && !isset( $sort[ $b->term_id ] ) )
            $res = -1;
        else
            $res = 0;

        return $res;
    } );

    return $category;
}

print_r( get_sorted_categories() );
print_r( get_sorted_categories('date') );
print_r( get_sorted_categories('modified') );

按发布ID |发布日期|发布修改日期获取类别订单。没有任何循环和快速

我会首先检索所有类别,然后循环浏览类别,根据发布日期获取最后一篇文章,并将其存储在数组中,然后订购生成的数组感谢您的帮助!我的网站有数千篇文章和数十个类别,当然这会影响功能,请注意,您的输入错误为“orderby”=“post_date”。应该是'orderby'=>'post_date',并且返回的代码数组_key_存在:第一个参数应该是字符串或整数。我修改了代码,以便可以添加一些$args来获取_类别,但这破坏了排序过程,知道怎么做吗?再次感谢!嗨,tinyCoder。为句柄异常更新的代码hide empty=>false。但是关于其他$args,您真的需要传递$args才能在这个问题中获得_categories函数吗?感谢您的修改,我只需要显示空类别并排除一个类别。@tinyCoder代码已更新,现在支持显示_empty&exclude args。再试一次…老兄,你太棒了!!超级功能,我感谢你的帮助。