dpaal28266 2015-08-20 13:30
浏览 38
已采纳

WordPress get_results按发布日期排序

I'm using "get_results" in WordPress for the first time and need to order the results by date. What I've got so far is:

$wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );

This works fine in retrieving the posts, although it displays them with the oldest at the top of the list, instead of the most recent.

What's the correct syntax that I should be using in that query to reverse the order?

Thanks.

In case it's more useful, here's the full code:

<?php
                // Get years that have posts
                $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY year DESC" );

                //  For each year, do the following
                foreach ( $years as $year ) {

                    // Get all posts for the year
                    $posts_this_year = $wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );

                    // Display the year as a header
                    echo '<div class="year-header-block">';
                    echo '<button class="btn-year-reveal">View</button>';
                    echo '<h3>' . $year->year . ' Industry News &amp; Comment</h3>';
                    echo '</div>';

                    // Start an unorder list
                    echo '<ul class="no-list yearly-archive-list">';

                    // For each post for that year, do the following
                    foreach ( $posts_this_year as $post ) {
                        // Display the title as a hyperlinked list item
                        echo '<li><a href="' . get_permalink($post->ID) . '"><span class="news-title">' . $post->post_title . '</span><span class="news-date">' . get_the_time('F j, Y', $post->ID) . '</span></a></li>';
                    }

                    // End the unordered list
                    echo '</ul>';
                }
            ?>
  • 写回答

2条回答 默认 最新

  • duanbimo7212 2015-08-20 13:47
    关注

    You should ORDER BY xxx ASC instead of DESC to get the oldest first as follows:

    $years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' GROUP BY post_date ASC" );
    

    Also, if you want to order the other query, it should look like this:

    $wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "' ORDER BY post_date ASC" );
    

    Note: by default SQL always orders ascendantly, so you can remove the instruction ASC. But if you want it order older first you replace ASC for DESC.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?