简体   繁体   English

我如何获得分类法类别,以便他们的帖子日期在wordpress中从最新到最旧

[英]How do i get taxonomy categories in order to their posts date in wordpress from latest to oldest

I have Video categories in my website homepage. 我的网站主页上有视频类别。 Have a taxonomy called video-category and custom post type called video-list . 有一个名为视频类别的分类和称为视频列表的自定义帖子类型。

I need to display the taxonomy categories in order to its posts date from latest to oldest . 我需要显示分类法类别,以便其帖子的日期最新到最旧 So that means if i publish one video post today that current post category must be on top as its post have latest date and the rest should go from latest to oldest. 这意味着,如果我今天发布一个视频帖子,那么当前的帖子类别必须位于顶部,因为它的帖子有最新日期,其余的应该从最新到最旧。

I don't know from where i need to start. 我不知道从哪里开始。 Started with getting the latest post and don't know what's the next. 开始获取最新帖子,不知道下一步是什么。

What i currently have is displaying them by most viewed with the below code 我目前所拥有的是通过以下代码查看最多显示它们

<ul class="catLists">
                          <?php
                        $video_args = array(
                            'hide_empty' => true,
                            'fields' => 'all',
                            'hierarchical' => true,
                            'child_of' => 0,
                            'get' => '',
                            'name__like' => '',
                            'pad_counts' => false,
                            'taxonomy' => 'video-category',
                            'cache_domain' => 'core'
                        );
                        $v_terms = get_terms('video-category', $video_args);
                        $coount = 1;
                        global $wpdb;
                        foreach($v_terms as $key => $term) {
                            $count_views = $wpdb->get_results($wpdb->prepare("
                                SELECT SUM(pm.meta_value) AS view_count FROM {$wpdb->postmeta} pm
                                LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
                                LEFT JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
                                LEFT JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
                                WHERE pm.meta_key = 'post_views_count'
                                AND tt.taxonomy = 'video-category'
                                AND tt.term_id = %d
                            ", $term->term_id));

                            // Add to the category object the result
                            $v_terms[$key]->count_views = (!empty($count_views)) ? $count_views[0]->view_count : 0;


                        } 
                        function cmp($a, $b) {
                            if ($a->count_views == $b->count_views) {
                                return 0;
                            }
                            return ($a->count_views > $b->count_views) ? -1 : 1;
                        }
                        usort($v_terms, "cmp");
                        foreach ($v_terms as $v_term) {
                            $term_link = get_term_link($v_term, 'video-category');
                            if($coount < 7){ 
                            ?>
                    <li class="video_<?php echo $v_term->term_id; ?>_term" id="<?php echo $coount++ ?>">
                        <a href="<?php echo $term_link ;?>" id="<?php echo $v_term->term_id; ?>"><?php echo $v_term->name; ?></a>
                        <span class="active_tab_border"></span>
                    </li>
                        <?php  }else{?>
                 <?php if($coount == '7') { ?>
                 </ul>

The desired result is to display first the category which post has the latest published date and the rest etc. 期望的结果是首先显示哪个帖子具有最新发布日期和其余等的类别。

Thanks in advance for the help. 先谢谢您的帮助。

You can do it with a single WP_Query 你可以使用一个WP_Query来完成它

https://codex.wordpress.org/Class_Reference/WP_Query https://codex.wordpress.org/Class_Reference/WP_Query

$args = array(
    'post_type' => 'video-list',
    'orderby'    => 'date',
    'category_name' => 'video-category'
);
$query = new WP_Query( $args );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM