get_categories按上一篇文章排序

时间:2017-03-29 17:40:46

标签: wordpress

按照上次发布的文章排序的get_categories Wordpress中最好和最短的方法是什么?

意味着具有最近帖子的类别应首先出现,是否可以某种方式提供?

2 个答案:

答案 0 :(得分:6)

试一试:

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 |发布日期|发布修改日期)。没有任何循环和快速!

答案 1 :(得分:1)

我认为最简单的方法是遍历<?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

?>

当然,正如上面的评论中所提到的,您也可以反过来循环并首先遍历类别然后对数组进行排序。这样的事情可以做到:

register_post_type( 'Communities',
    array(
        'labels' => array(
        'name' => __( 'Communities' ),
        'singular_name' => __( 'Community' ),
        'not_found' => __( 'No Communities Found' )
        ),
        'public' => true,
        'rewrite' => array( 'slug' => '/%category%/communities', 'with_front' => false),
        'has_archive' => true,
        'hierarchical'  => true,
        'taxonomies'  => array( 'category' ),
        'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields' ),
        'menu_icon' => 'dashicons-admin-home'
    )
);