0

I'm running into some issues while working with a custom post type (CPT). The problem is that the permalinks of the CPT are fine for a single post but not for the archive/overview page.

The website that I'm working should have an archive page with all kind of clothes (CPT) categorized by brand.

The slug of the CPT should be website.com/cloths/brand-name/clothing-name. That's all working fine. Also the taxomomy overview pages like website.com/cloths/brand-name are fine. However the overview/archive page is broken and returns a 404 instead of website.com/cloths/

I really don't know why this is happening especially when the taxonomy and single post permalinks are all fine. I've already changed the permalink settings but without any result. If I create a custom page template with the code that I'm using on the archive template the permalink is fine for the verview/archive page but the pagination is broken, the 2nd page returns a 404.

This is the code that I'm using to create the CPT and desired permalink structure:

function my_custom_post_product() {
$labels = array(
    'name'               => _x( 'Collectie', 'post type general name' ),
    'singular_name'      => _x( 'Kleding stuk', 'post type singular name' ),
    'add_new'            => _x( 'Nieuw kleding stuk', 'reis' ),
    'add_new_item'       => __( 'Nieuw kleding stuk toevoegen' ),
    'edit_item'          => __( 'Kleding stuk wijzigen' ),
    'new_item'           => __( 'Nieuw item' ),
    'all_items'          => __( 'Alle kleding stukken' ),
    'view_item'          => __( 'Bekijk item' ),
    'search_items'       => __( 'Doorzoek collectie' ),
    'not_found'          => __( 'Geen kleding stuk gevonden  ' ),
    'not_found_in_trash' => __( 'Geen kleding stuk gevonden in de prullenbak' ),
    'parent_item_colon'  => '',
    'menu_name'          => 'Collectie'
);
$args = array(
    'labels'        => $labels,
    'description'   => 'Nieuw kleding stuk toevoegen',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
    'hierarchical'  => true,
    'rewrite'       => array('slug' => 'kleding/%merk%','with_front' => false),
    'query_var'     => true,
);
register_post_type( 'kleding', $args );
}
add_action( 'init', 'my_custom_post_product' );

/* Cloths custom taxonomy */
function my_taxonomies_product() {
$labels = array(
    'name'              => _x( 'Merken', 'taxonomy general name' ),
    'singular_name'     => _x( 'Merk', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Product Categories' ),
    'all_items'         => __( 'Alle merken' ),
    'parent_item'       => __( 'Parent Product Category' ),
    'parent_item_colon' => __( 'Parent Product Category:' ),
    'edit_item'         => __( 'Edit Product Category' ),
    'update_item'       => __( 'Update Product Category' ),
    'add_new_item'      => __( 'Nieuw merk toevoegen' ),
    'new_item_name'     => __( 'New Product Category' ),
    'menu_name'         => __( 'Merk' ),
);
$args = array(
    'labels' => $labels,
    'hierarchical'  => true,
    'public'        => true,
    'query_var'     => 'merk',
    'rewrite'       =>  array('slug' => 'kleding' ),
    '_builtin'      => false,
);
register_taxonomy( 'merk', 'kleding', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );

/* Permalink filter Collectie */
add_filter('post_link', 'brand_permalink', 1, 3);
add_filter('post_type_link', 'brand_permalink', 1, 3);

function brand_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%merk%') === FALSE) return $permalink;
    // Get post
    $post = get_post($post_id);
    if (!$post) return $permalink;

    // Get taxonomy terms
    $terms = wp_get_object_terms($post->ID, 'merk');
    if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
        $taxonomy_slug = $terms[0]->slug;
    else $taxonomy_slug = 'geen-merk';

return str_replace('%merk%', $taxonomy_slug, $permalink);
}

Please note that some parts of the code above are in Dutch. 'merk' = 'brand-name' and 'kleding' = 'cloths'

2
  • 1
    have you tried setting has_archive to the desired slug rather than just true?
    – Milo
    Nov 17, 2014 at 15:40
  • Thanks so much! Changing is to the slug resolved the 404 and now it's also using the correct archive template.
    – NielsPilon
    Nov 18, 2014 at 14:46

1 Answer 1

0

I noticed that you haven't connected your custom post type and taxonomy completely. I'm not sure if this is causing your current issue, but it would be the first thing I would try.

From the Codex:

When registering a post type, always register your taxonomies using the taxonomies argument. If you do not, the taxonomies and post type will not be recognized as connected when using filters such as parse_query or pre_get_posts. This can lead to unexpected results and failures.

So, I would add to your post-type args:

'taxonomies' => array('merk'),

and see if that fixes your problem.

1
  • I've added it but it didn't fixes the problem. But the suggestion about the archive slug posted above solved the problem.
    – NielsPilon
    Nov 18, 2014 at 14:49

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.