Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

404 on Custom Post Type Archive WordPress

  • SOLVED

I built a website that uses 12 different custom post types. Here's an example of the code generating each of the custom post types.

// REGISTER CUSTOM POST TYPE FOR DESTINATIONS
function destinations_post_type() {
register_post_type('destinations',array(
'labels' => array(
'name' => 'Destinations',
'singular_name' => 'Destination',
'menu_name' => 'Destinations',
'add_new_item' => __('Add New Destination'),
'edit' => __('Edit'),
'edit_item' => __('Edit Destination'),
'new_item' => __('New Destination'),
'view' => __('View Destination'),
'view_item' => __('View Destination'),
'search_items' => __('Search Destinations'),
'not_found' => __('No Destinations found'),
'not_found_in_trash' => __('No Destinations found in Trash'),
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'supports' => array('title','editor','thumbnail', 'custom-fields'),
'menu_position' => 25,
'rewrite' => array(
'slug' => 'destinations',
'with_front' => false
),
'capability_type' => 'post',
'hierarchical' => false,
'query_var' => true,
'has_archive' => 'destinations'
));
}
add_action('init', 'destinations_post_type');


I get 404 errors when I visit the archive pages. They go away once I visit the 'Permalinks' page in the admin panel but after about a week it's back to the 404 pages.

I've dug around quite a bit about these 404s and the only solution I find are to visit the 'Permalinks' page. This solution does help but it's only temporary. I need to fix this issue permanently.

Thanks in advance for the help.

Regards,
Denis

Answers (6)

2011-05-06

Michael Fields answers:

Hi Denis,

You have:

add_action('init', 'destinations_post_type');

I would suggests using:

add_action('init', 'destinations_post_type', 0 );

Instead.

What I believe is happening here is that you have a plugin or some other custom code that is flushing the rewrite rules before the post type is registered with WordPress. Hooking the registration code as early as possible should avoid this.


Denis Leblanc comments:

Hi Micheal,

So, what exactly is the zero doing? Just curious.


Michael Fields comments:

By default, WordPress registers all hooks at "10". So your init hook is registered at ten as well. It is possible that another plugin has it's own function registered during int 10 that is firing before your custom post type registration hook. This function may be flushing the rewrite rules before your code has a change to register the post_type. This is the only scenario that I can think of as to why the custom post_type archive pages would "automatically" start sending 404s at random times.


Michael Fields comments:

Oh, yeah ... The zero makes sure that the function fires as early as possible during the hook.


Denis Leblanc comments:

Thanks Michael,

I've added the zero and visited the permalinks page. Everything works again but time will tell whether it'll be a permanent fix or not.

Thanks for clarifying what is possibly happening here.

Denis


Denis Leblanc comments:

Michael,

The problem hasn't reoccurred so it looks like your solution might have fixed the problem.

Thanks again.

Denis

2011-05-06

Hameedullah Khan answers:

I think it was not a good advice after all.

2011-05-06

Milan Petrovic answers:

Never use flush_rewrite_rules() in the init(), it will run every time page is loaded and recreating rules can take a lot of time. If you are adding custom post types or taxonomies manually, best is to use Settings -> Permalinks pages, or to manually and only after making changes run flush_rewrite_rules() function.

My GD Custom Posts & Taxonomies Tools plugin (both free and pro versions) are performing rules flushing only when changes are made to post type or taxonomy and don't disrupt the normal page processing: http://d4p.me/gdtt


Denis Leblanc comments:

Thanks Milan,

I definitely don't need another plugin for this site.

2011-05-07

Anna-marie Redpath answers:

Dont know whether it helps, but there is a wp bug where it 404's if the archive is empty

http://core.trac.wordpress.org/ticket/17316

patch - see last version


also some funnies:

Setting permalinks programatically breaks custom post type URL's

http://core.trac.wordpress.org/ticket/16786#comment:1


Denis Leblanc comments:

Hi Anna-marie,

The archives are not empty.

Thanks.

2011-05-07

Just Me answers:

I don't think there is anything wrong with the way you have set things up. And yes the permalink page is your friend in solving this.

You should focus on what is causing it to not work after a week.
Any update, (caching) plugin, anything, that might be doing something to your WP installation?


Denis Leblanc comments:

Yes, the permalink page is my friend at the moment but the site isn't even live yet and I'm already sick or visiting it. I can't imagine having to tell my client that he has to visit the page at a minimum of once a week. I'm pretty sure he'd be finding himself a new developer faster then he can revisit the permalinks page.

The reason for this question is to get to the root of it.

Here's the list of plugins I'm using on this site:
<strong>BWP Google XML Sitemaps
cforms
Custom Field Template
External Links
Flexible Lightbox
uBillboard
WordPress FAQs</strong>

Thanks for your time.


Just Me comments:

not familiar with all the plugins but I'll take a look at them. What does your permalink setting look like? Do you see any difference when things start not working? Different redirect or something?


Just Me comments:

and while we are at it, what does your .htaccess file look like?


Denis Leblanc comments:

Permalink structure is: /%postname%

.htacces file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


Just Me comments:

your permallink setting and .htaccess file seem to be ok.

Are you able to test things right now, as it seems to not work pretty randomly.

I wonder what would happen if you created an archive_destinations.php template file (if none is present already).

Is it possible to take a look at this website?


Denis Leblanc comments:

I've already got an archive-destinations.php template.

I've implemented the suggested fix from Michael in the first comment above so it's just a waiting game now to see if the errors will happen again. The problem seemed to repeat itself about a week after I would visit the permalinks page.

I have no way of making the errors happen manually.


Denis Leblanc comments:

Here's the link to the site: http://jacksonsafricansafaris.com/


Just Me comments:

Thanks for the information. Nothing weird there. Did you contact your host? See if they are doing anything on a weekly basis?


If you are not able to reproduce the error then it is going to be a long way to find out what is going on. I hope the change you made will proof to be the right one. i am a little pessimistic coz things seem to be working fine for a week without that change.

You will have more luck finding the problem when you can troubleshoot while it is happening. Try different solutions without resaving the permalinks.

Good luck!

2011-05-09

David Navarrete answers:

add this before end of the function

$wp_rewrite->flush_rules();

and add this code after declaring the function

global $wp_rewrite;


greatings