WordPress.org

Ready to get started?Download WordPress

Forums

Empty Archives produce 404 - FIX in core necessary (1 post)

  1. IM_natascha
    Member
    Posted 5 months ago #

    Dear community,

    I cannot search for an open thread regarding this topic, so I have to open one regardless.

    The Problem is easy: If you go to an archive url, lets say /2011/ or /2013/12/ (yes, the latter has not even passed yet), and both are empty, both would send a 404 status header. Which is semantically incorrect. The archive does exist, it just is empty for this type of request.

    I have yet not considered ALL types of archives, but: regarding yearly, montly or daily archives, following code should fix the issue.
    file: wp-includes/class-wp.php

    function handle_404() {
    	global $wp_query;
    
    	// If we've already issued a 404, bail.
    	if ( is_404() )
    		return;
    
    	// Never 404 for the admin, robots, or if we found posts.
    	if ( is_admin() || is_robots() || $wp_query->posts ) {
    		status_header( 200 );
    		return;
    	}
    
    	// We will 404 for paged queries, as no posts were found.
    	if ( ! is_paged() ) {
    
    		// Don't 404 for these queries if they matched an object.
    		if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
    			status_header( 200 );
    			return;
    		}
    
    		// Don't 404 for these queries either.
    		if ( is_home() || is_search() ) {
    			status_header( 200 );
    			return;
    		}
    	}
    
    	// FIX: archives are no 404 just because they are empty!
    	if(is_archive()) {
    		status_header( 200 );
    		return;
    	}
    
    	// Guess it's time to 404.
    	$wp_query->set_404();
    	status_header( 404 );
    	nocache_headers();
    }

    Possible adaption: put the is_archive() condition next to is_search()

    I'd love to avoid messing with the core, but here there is no other possibility yet. Please insert hooks to allow status-header manipulation/correction.

    Hope to help someone,
    best regards,
    Natascha

Reply

You must log in to post.

About this Topic

  • RSS feed for this topic
  • Started 5 months ago by IM_natascha
  • This topic is not a support question
  • WordPress version: 3.6