Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress按发布日期获取结果订单_Php_Wordpress - Fatal编程技术网

Php WordPress按发布日期获取结果订单

Php WordPress按发布日期获取结果订单,php,wordpress,Php,Wordpress,我第一次在WordPress中使用“get_results”,需要按日期排序结果。到目前为止,我得到的是: $wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" ); 这可以很好地检索帖子,尽管它会在列表顶部显示最早的帖子,而不是最近的帖子 我应该在该查

我第一次在WordPress中使用“get_results”,需要按日期排序结果。到目前为止,我得到的是:

$wpdb->get_results( "SELECT * FROM asi_posts WHERE post_type = 'post' AND post_status = 'publish' AND YEAR(post_date) = '" . $year->year . "'" );
这可以很好地检索帖子,尽管它会在列表顶部显示最早的帖子,而不是最近的帖子

我应该在该查询中使用什么正确语法来颠倒顺序

谢谢

如果它更有用,下面是完整的代码:

<?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>';
                }
            ?>

您应该
按xxx ASC
而不是
DESC
订购,以获得最早的订单,如下所示:

$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" );
此外,如果要对另一个查询进行排序,它应该如下所示:

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

注意:默认情况下,SQL总是以升序排序,因此您可以删除指令ASC。但是,如果您想先订购较旧的,您可以将ASC替换为DESC。

您应该按xxx ASC
而不是
DESC
订购最旧的ASC,如下所示:

$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" );
此外,如果要对另一个查询进行排序,它应该如下所示:

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

注意:默认情况下,SQL总是以升序排序,因此您可以删除指令ASC。但是,如果您想先订购旧的,您可以用ASC代替DESC。

我们可以用两种方法来完成

来自您的代码。

更改下面的代码

// 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 . "'" );
到下面的代码

// 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 . "' ORDER BY post_date DESC" );
其他解决方案:

在主题的function.php文件中创建一个新函数按年份发布

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish'
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);

  return $years;
}
并在模板中调用以下代码

<?php foreach(posts_by_year() as $year => $posts) : ?>
  <h2><?php echo $year; ?></h2>

  <ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      </li>
    <?php endforeach; ?>
  </ul>
<?php endforeach; ?>


我们可以通过两种方式来实现

来自您的代码。

更改下面的代码

// 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 . "'" );
到下面的代码

// 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 . "' ORDER BY post_date DESC" );
其他解决方案:

在主题的function.php文件中创建一个新函数按年份发布

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish'
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);

  return $years;
}
并在模板中调用以下代码

<?php foreach(posts_by_year() as $year => $posts) : ?>
  <h2><?php echo $year; ?></h2>

  <ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      </li>
    <?php endforeach; ?>
  </ul>
<?php endforeach; ?>


您是否尝试先显示上一次添加的帖子,然后再显示某一特定年份的帖子?您是否尝试先显示上一次添加的帖子,然后再显示某一特定年份的帖子?这非常有用,因为它非常适合本网站的另一个方面,我需要对显示内容进行更多控制。再次感谢。这真的很有用,因为它很适合这个网站的另一个方面,我需要更多地控制显示的内容。再次感谢。