最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wpdb - Display future posts in archive

programmeradmin1浏览0评论

I'm using the following code to display an archive of posts.

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_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 wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND post_status = 'future' AND YEAR(post_date) = '" . $year->year . "' ORDER BY post_date DESC" );

        foreach ( $posts_this_year as $post ) {
            // echoing miscellaneous stuff
        }
}
?>

There it says post_status = 'publish'. How can I add future posts to my archive? I tried adding AND post_status = 'future' to both $wpdb->get_results entries but it doesn't work.

I'm using the following code to display an archive of posts.

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_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 wp_posts WHERE post_type = 'post' AND post_status = 'publish' AND post_status = 'future' AND YEAR(post_date) = '" . $year->year . "' ORDER BY post_date DESC" );

        foreach ( $posts_this_year as $post ) {
            // echoing miscellaneous stuff
        }
}
?>

There it says post_status = 'publish'. How can I add future posts to my archive? I tried adding AND post_status = 'future' to both $wpdb->get_results entries but it doesn't work.

Share Improve this question asked Sep 4, 2014 at 15:31 descentropydescentropy 51 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

You need to use "OR" instead of "AND",

<?php
// Get years that have posts
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = 'post' AND (post_status = 'publish' or post_status = 'future') 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 wp_posts WHERE post_type = 'post' AND (post_status = 'publish' OR post_status = 'future') AND YEAR(post_date) = '" . $year->year . "' ORDER BY post_date DESC" );

        foreach ( $posts_this_year as $post ) {
            // echoing miscellaneous stuff
        }
}
?>

Also this one works:

function wpsites_query( $query ) {
if ( $query->is_archive() && $query->is_main_query() && !is_admin() ) {
    $query->set( 'post_status', array('publish','future') );
  }
}
add_action( 'pre_get_posts', 'wpsites_query' );
发布评论

评论列表(0)

  1. 暂无评论