[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:55:21 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):

 A more complete approach:


 {{{
 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( $year )
                 {
                         $wp_query->is_year = 1;
                         $wp_query->is_month = 0;
                         $wp_query->is_day = 0;
                 }

                 if( $monthnum )
                 {
                         $wp_query->is_month = 1;
                         $wp_query->is_day = 0;
                         $wp_query->is_year = 0;
                 }

                 if ( $day )
                 {

                         $wp_query->is_day = 1;
                         $wp_query->is_year = 0;
                         $wp_query->is_month = 0;
                 }

                 if( $monthnum && !$day )
                         $day = 1;

                 if( $year && !$monthnum )
                 {
                         $monthnum = 1;
                         $day = 1;
                 }

                 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;
                         $wp_query->is_date = 1;
                 }
         }
 }

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

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


More information about the wp-trac mailing list