4

I made a custom post type with taxonomies and when I go to the taxonomy page and view one of the taxonomies I get a page not found error like the taxonomy wasnt made.

Any suggestions?

/*Products*/
$labels = array(
    'name' => _x('Products', 'post type general name'),
    'singular_name' => _x('Product', 'post type singular name'),
    'add_new' => _x('Add New', 'Product'),
    'add_new_item' => __("Add New Product"),
    'edit_item' => __("Edit Product"),
    'new_item' => __("New Product"),
    'view_item' => __("View Product"),
    'search_items' => __("Search Product"),
    'not_found' =>  __('No products found'),
    'not_found_in_trash' => __('No products found in Trash'), 
    'parent_item_colon' => ''
  );

$args = array(
    'labels' => $labels,
        'singular_label' => __('Products'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'query_var' => 'products',
        'taxonomies' => array('products-cat'),
        'supports' => array('title', 'editor', 'custom-fields', 'thumbnail', 'excerpt'),
        'has_archive' => true,
);
register_post_type( 'products' , $args );

// Add to admin_init function
add_filter('manage_edit-products_columns', 'add_new_products_columns');
function add_new_products_columns($columns) {
$columns = array(
    'cb' => '<input type="checkbox" />',
        'images' => 'Images',
    'title' => 'Product Name',
        'author' => 'Author',
        'product-categories' => 'Categories',
        'tags' => 'Tags',
        'date' => 'Date',
); 
    return $columns;
}

add_action('manage_products_posts_custom_column', 'manage_products_columns', 10, 2);

function manage_products_columns($column_name, $id) {
    global $wpdb;
    switch ($column_name) {

    case 'images':
            //echo get_the_post_thumbnail( $page->ID, array(50,50) ); 
    break;

    case 'product-categories':
        echo get_the_term_list($post->ID, 'products-cat', '', ', ','');
    break;

        default:
        break;
    } 
}

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

function products_create_taxonomies() 
{
    // Photo Categories
    register_taxonomy('products-cat',array('products'),array(
        'hierarchical' => true,
        'label' => 'Product Categories',
        'singular_name' => 'Products Category',
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'products' )
    ));
}

/*End Products*/

I have this in my archives page

archive-products.php

<?php get_header(); ?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();?>
                <?php the_title(); ?>   
<?php endwhile; endif; ?>

4 Answers 4

17

I encountered the same problem on my local nginx platform. After I updated permalinks in the settings, everything worked fine. Settings > Permalinks > Save Changes (without modification)

3
  • Try this suggested solution before you do anything else. This basically works for me every time.
    – Braza
    Jul 21, 2020 at 10:12
  • Wordpress is so stupid.
    – serraosays
    Oct 13, 2020 at 21:41
  • I tried this more than 10 times but it's not working for me. It's showing a page not found. Jul 4, 2021 at 16:56
2

After some research I have found a blog who actually had an answer to this problem.

Here is the function along with the blog url.

function taxonomy_slug_rewrite($wp_rewrite) {
    $rules = array();
    // get all custom taxonomies
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    // get all custom post types
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');

    foreach ($post_types as $post_type) {
        foreach ($taxonomies as $taxonomy) {

            // go through all post types which this taxonomy is assigned to
            foreach ($taxonomy->object_type as $object_type) {

                // check if taxonomy is registered for this custom type
                if ($object_type == $post_type->rewrite['slug']) {

                    // get category objects
                    $terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));

                    // make rules
                    foreach ($terms as $term) {
                        $rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
                    }
                }
            }
        }
    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');
1
  • Blog URL not working Jul 4, 2021 at 16:57
1

I'd recommend at least two things:

  1. Change the priority for your products_create_taxonomies action to at least 1, if not 5 or higher. (I'm thinking 0 is not valid for action priorities for some reason)

  2. Add your post type creation into an init action just like you did your taxonomies at an earlier priority than your taxonomy (since you are connecting them via the taxonomy).

If those two changes don't take care of it, try setting up both (without linking them via their own structures) and then call the following on a later priority (still during init):

register_taxonomy_for_object_type( 'products-cat', 'products' );

That should make sure that they are fully and properly linked up.

0

I think your main issue here is the rewrite for your taxonomy. As it stands, your rewrite for the taxonomy looks like this

'rewrite' => array('slug' => 'products' )

This is what exactly what this rewrite means in your custom post type. The following

'rewrite' => true,

translates to this in your custom post type

'rewrite' => array('slug' => 'products' )

By default, this clash in rewrites will 404 your taxonomy requests. As a solution, you can change your rewrite for your taxonomy, or have a look at this post I have done recently on this site on a better workaround to this.

Important: Flush your permalinks after every update in your code to reset your permalinks to your new structure

5
  • that fixed it but the slug name is changed to the registered taxonomy which is products-cat. How can i keep the rewrite true while having the slug name to products? Jan 23, 2015 at 7:05
  • Have you checked the linked post. Same rewrite slugs for both custom post types and custom taxonomies is always a problem to get working. If my mind served me correctly, the user who posted the question which I answered have done a post on that exact same issue. Just look through his posts as well Jan 23, 2015 at 7:09
  • Ok I looked over the post that you recommended an changed from using dashes to underscores and downloaded the permalink plugin. And after making these changes the I realize that changing the rewrite the code from what I have to making it true isnt the same. when i make it true makes the url the same as the registered taxonomy name. the url also displays the custom post type name and the registered taxonomy name for example is products/products-cat/automotive/. Any suggestions? Jan 23, 2015 at 8:20
  • To be honest, this is as far as I can help you unfortunately. Frankly, with rewrites, we are in the same boat :-). I know having the same URL's for taxonomy pages and custom post type archives are possible with extra rewrite rules. I think with a custom rewrite rule, you can remove the taxonomy name in the URL structure in the previous comment you posted. Unfortunately, I haven't looked into doing something like this myself. I hope you get this solved, and if you do, please post your solution and accept your own answer. This will be really useful for future users Jan 23, 2015 at 8:42
  • Thanks, I will continue to search for a way and post back here when i find an answer. Jan 23, 2015 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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