WordPress.org

Ready to get started?Download WordPress

Forums

Empty Archives Returning 404 (8 posts)

  1. nrutman
    Member
    Posted 3 years ago #

    Is anyone else having this problem?

    I've setup my custom post type with Custom Post Type UI. I've turned on rewriting, archiving, and given it a slug. Both individual posts and the archives page is working great.

    EXCEPT, it looks like querying the archive page will result in a 404 if there are no posts of the type available. Note, this is not the ELSE on the loop that checks for have_posts() on the archive page...it's not even getting that far. It's returning the 404 page template as if the page can't be found.

    Is this a known bug? How do I load up an "empty" archive telling people to check back soon for more content? This particular instance is an event type, so if there aren't events it shouldn't result in a 404.

    Any ideas?
    -Nate

  2. danderozier
    Member
    Posted 3 years ago #

    This is happening for me too—for empty custom post type archives, rather than setting have_posts() to false, it just results in a 404.

    Can't seem to find any trac tickets about this issue. Does anyone in the community know more about it?

  3. Drew Gourley
    Member
    Posted 3 years ago #

    Hey guys, ran into this myself with some hard work created this solution:

    You can toss this into functions.php in your template and it will give you the desired effect.

    function dg_override_404() {
    	global $wp_query;
    	$args = array( 'public' => true, '_builtin' => false );
    	$post_types = get_post_types($args);
    	if ( in_array($wp_query->query_vars[post_type], $post_types) && $wp_query->is_404 == true ) {
    		$wp_query->is_404 = false;
    	}
    }
    add_action('pre_get_posts', 'dg_override_404');
    function dg_override_template() {
    	global $wp_query;
    	$args = array( 'public' => true, '_builtin' => false );
    	$post_types = get_post_types($args);
    	if ( in_array($wp_query->query_vars[post_type], $post_types) && $wp_query->is_404 == true ) {
    		require TEMPLATEPATH.'/archive.php';
    		exit;
    	}
    }
    add_action('wp', 'dg_override_template');

    Basically what this does is only firing when your on an non-built-in post type that is returning a 404 error, which is only the case on empty post type archive pages. It switches the is_404 argument to true and forces it to use the archive template. I'll investigate this more tomorrow, because it probably requires some refinement.

  4. Drew Gourley
    Member
    Posted 3 years ago #

    Further Investigation reveals this to be a more complete solution:

    function dg_override_404() {
    	global $wp_query;
    	$args = array( 'public' => true, '_builtin' => false );
    	$post_types = get_post_types($args);
    	if ( in_array($wp_query->query_vars[post_type], $post_types) && $wp_query->is_404 == true ) {
    		$wp_query->is_404 = false;
    		$wp_query->is_archive = true;
    		$wp_query->is_post_type_archive = true;
    	}
    }
    add_action('pre_get_posts', 'dg_override_404');
    function dg_override_template() {
    	global $wp_query;
    	$args = array( 'public' => true, '_builtin' => false );
    	$post_types = get_post_types($args);
    	if ( in_array($wp_query->query_vars[post_type], $post_types) && $wp_query->is_404 == true ) {
    		include(TEMPLATEPATH.'/archive.php');
    		exit;
    	}
    }
    add_action('template_redirect', 'dg_override_template');

    I still need to find a way to change the headers to 200 instead of 404 and to circumvent the 'Page not found' from showing up in the title of the page.

  5. veganista
    Member
    Posted 3 years ago #

    Drew,

    Did you get any further improving this? I cant get rid of the status code or page title either.

    I have made a small adjustment to your code so that custom archive templates will get loaded if they exist.

    [Code moderated as per the Forum Rules. Please use the pastebin]

  6. davidjbullock
    Member
    Posted 3 years ago #

    Drew, thanks for doing the heavy lifting.

    Here's my tweak to load the custom archive template. Probably needs some additional love.

    <iframe src="http://pastebin.com/embed_iframe.php?i=9EbfAqrh" style="border:none;width:100%"></iframe>

  7. davidjbullock
    Member
    Posted 3 years ago #

    Veganista,

    Add $wp_query->is_404 = false; to the 2nd part where it pulls the custom post archive. That will de-404 it.

    Dave

  8. Gustavo Bordoni
    Member
    Posted 3 years ago #

    Hi Guys,

    This problem is going to be solved in the version 3.2.

    But if you really need it now you need to change the core as said in the ticket page.

    Cya,

Topic Closed

This topic has been closed to new replies.

About this Topic