[wp-trac] [WordPress Trac] #18190: Don't redirect to 404 page on empty post archives

WordPress Trac wp-trac at lists.automattic.com
Fri Jul 22 13:10:58 UTC 2011


#18190: Don't redirect to 404 page on empty post archives
--------------------------+------------------------------
 Reporter:  gazpachu      |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:  3.2.1
 Severity:  normal        |  Resolution:
 Keywords:  needs-patch   |
--------------------------+------------------------------

Comment (by gazpachu):

 After some time, I've found a way to fix this:


 {{{
 function dg_override_404() {
         global $wp_query, $post;

         if ( $wp_query->post_count == 0 )
         {
                 $day = get_query_var( 'day' );
                 $monthnum = get_query_var( 'monthnum' );
                 $year = get_query_var( 'year' );

                 if ( $day || $monthnum || $year )
                 {
                         status_header( 200 );

                         $id=-28; // need an id
                         $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 =
 $year.'-'.$monthnum.'-'.$day.'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_archive = 1;
                 }
         }
 }

 add_filter('template_redirect', 'dg_override_404');
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18190#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list