0

hey im trying to display future posts in date page but date page redirect to 404 page when it is future date so i used this function

function dg_override_404() {
    global $wp_query, $post;
    if(is_year() || is_day()){
        $wp_query->is_404 = true;
    }
    if($wp_query->post_count == 0){
        $dayarchive = get_query_var('day');
        $monthnumarchive = get_query_var( 'monthnum' );
        $yeararchive = get_query_var( 'year' );
        if ( $dayarchive || $monthnumarchive){
            status_header( 200 );
            $id=-28;
            $post = new stdClass();
            $post->ID= $id;
            $post->post_content='';
            $post->post_excerpt= '';
            $post->post_status='publish';
            $post->post_title= '';
            $post->post_type='post';
            $post->post_date = $yeararchive.'-'.$monthnumarchive.'-'.$dayarchive.'00:00:00';
            $wp_query->queried_object=$post;
            $wp_query->post=$post;
            $wp_query->found_posts = 1;
            $wp_query->post_count = 1;
            $wp_query->is_404 = false;
            $wp_query->posts = array($post);
            $wp_query->is_date = 1;
        }
    }
}add_filter('template_redirect', 'dg_override_404');

it work but doesn't show the page title in browser and if i changed

$wp_query->is_date = 1; to $wp_query->is_archive = 1; work just fine but i need to use it in date page so is there anyway to do that,thanks

1

You can alter the query with pre_get_posts before it's run to include future posts:

function wpd_future_date_archives( $query ){
    if( $query->is_date() && $query->is_main_query() ) {
        $query->set( 'post_status', array( 'publish', 'future' ) );
    }
}
add_action( 'pre_get_posts', 'wpd_future_date_archives' );

The posts won't behave the same as published posts though- they have no pretty permalink until they're published.

  • what i mean is the date page url redirect to 404 not display the future posts if i go to link like that localhost:8000/wordpress/2017/09 the result is 404 page so i used the function in my question to force display the pages my issue is when using is_date the page doesn't display <title></title> but if used is_archive work just fine so i need soloution to do that with is_date – Mouner Mostafa May 27 '17 at 23:22
  • I understand your problem, and this solves that without having to use your code- it fixes the query before it's sent to the database so it never results in a 404. – Milo May 28 '17 at 1:18

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.