简体   繁体   中英

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

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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