0

I have an existing site that has a custom post type with no archive. It has been live for almost a year now. I am trying to change the custom post type to have an archive now.

So I changed has_archive to "true," went to Settings > Permalinks and saved it to flush rewrite rules, but when I go to the archive (example.com/recipes/) it is still a 404 page.

There is no caching plugin active.

The Types plugin was used to create the CPT.

There are also some custom rewrite rules in place for the recipes CPT, but I would think that this would only affect the single post, not the post archive:

function custom_rewrite_rules() {
    add_rewrite_rule('^recipes/([^/]*)/([^/]*)/?$' ,'index.php?recipes=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rules');

function custom_links($permalink, $post_id, $leavename) {
    $post = get_post($post_id);
    if ($post->post_type == 'recipes') {
        $cats = wp_get_object_terms($post->ID, 'recipe-category');
        if ($cats) {
            $tax_rec_cat = $cats[0]->slug;
        } else {
            $tax_rec_cat = 'all';
        }
        $permalink = str_replace('%tax_rec_cat%', $tax_rec_cat, $permalink);
    }
    return $permalink;
}
add_action('post_type_link', 'custom_links', 10, 3);

Again, I'm not having any issues with viewing a recipes taxonomy archive (example.com/recipes/appetizers/) or the single recipe post (example.com/recipes/appetizers/hummus/), just the main post archive (example.com/recipes/).

Could there be a place that disables pages or post archives that I am not looking?

  • Post the code you used to register the CPT. Have you tested without the extra rewrite rules, just to verify that that isn't the problem? – s_ha_dum Jul 22 '13 at 23:03
  • I used the plugin "Types" to create the CPT, so I don't have code to paste. Thanks for your suggestion with removing the rewrite rules to test. It helped me figure out the problem. I'll post the solution as an answer. – isabisa Jul 23 '13 at 12:32
2

So it turns out that when the CPT was registered, the following was used in the "rewrite" argument in register_post_type:

rewrite => "recipes/%tax_rec_cat%"

I don't know the intention of this, I suppose whoever wrote it thought it was necessary to include both that and the custom_links function I posted in the original question in order to include the taxonomy term in the permalink.

To fix the problem with the archive having a 404 error, I changed rewrite to true and removed the custom format. As soon as I flushed the rewrite rules, the CPT archive, taxonomy archive, and single post all load fine.

  • Haha, okay, after some more digging, it looks like that rewrite was necessary for WP to give the single posts to the correct permalink when combined with the '%tax_rec_cat%' string replace in the custom_links function. I can just change it to: str_replace('recipes/', 'recipes/'.$tax_rec_cat.'/', $permalink); and it works as intended. – isabisa Jul 23 '13 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.