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.

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
        }
}
?>
| improve this answer | |
0

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' );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.