0

I am currently running against a wall while dealing with custom taxonomies. My intent is so use a taxonomy for categories for my custom post type, as to not interfere with the default categories used in wordpress. I've got that working for now and can retrieve a custom post's categories using this method. The problem I am running into is displaying the posts when clicking on a category, meaning I probably have an issue with the archive page, as far as I've been told.

My current CPT setup - I am using a custom class to create post types and taxonomies, the options are more relevant:

$eunv_tilemanager = new Cnm_CPT(
    'eunv_tilemanager',
    array(
        'name'                  => __('Tilemanager', 'eunv'),
        'singular_name'         => __('Tilemanager', 'eunv'),
        'name_admin_bar'        => __('Tilemanager', 'eunv'),
        'add_new_item'          => __('Add new tile', 'eunv'),
        'edit_item'             => __('Edit tile', 'eunv'),
        'search_items'          => __('Search tiles', 'eunv'),
        'not_found'             => __('No tiles found', 'eunv'),
        'not_found_in_trash'    => __('No tiles found in Trash', 'eunv'),
    ),
    array(
        'menu_icon'             => get_template_directory_uri() . "/assets/dist/img/eunv-logo-menu.png",
        'show_ui'               => true,
        'public'                => true,
        'publicly_queryable'    => false,
        'query_var'             => false,
        'rewrite'               => false,
        'has_archive'           => false,
        'hierarchical'          => false,
        'exclude_from_search'   => true,
        'show_in_nav_menus'     => false,
        'supports'              => array( 'title', 'thumbnail' ),
    )
);
$eunv_tilemanager->add_taxonomy(
    'eunv_tilemanager_cat',
    array(
        'name'                  => __('Categories', 'eunv'),
        'singular_name'         => __('Category', 'eunv'),
        'menu_name'             => __('Categories', 'eunv'),
    ),
    array(
        'show_ui'               => true,
        'public'                => true,
        'publicly_queryable'    => false,
        'query_var'             => true,
        'rewrite'               => array(
            'slug' => 'posts'
        ),
        'has_archive'           => true,
        'hierarchical'          => true,
        'exclude_from_search'   => true,
        'show_in_nav_menus'     => false,
        'show_admin_column'     => true
    )
);

I've tried around changing the taxonomies rewrite, query_var, exclude_from_search and public options. I am trying to get a link like '127.0.0.1/wp-project/posts/cat-name' to work, where 'posts' isn't the word I necessarily need as slug, it could be anything.

Right now, after using the following snippet and with the above setup, I can call that exact link, but I get redirected to the base page immediately:

add_action('init', 'custom_taxonomy_flush_rewrite');
function custom_taxonomy_flush_rewrite() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

I would appreciate any form of help, as I'm kind of running out of ideas and haven't found any applicable solution to my problem.

Thanks in advance and best regards

Nick

1 Answer 1

0

Apparently setting the option publicly_queryable of the taxonomy to true, then flushing the rewrite, then updating the permalinks solved the issue. I don't quiet understand why that's the solution, however.

3
  • If you had to update permalinks after, then your code that flushes rewrites runs before you register the taxonomy. You should also remove the code that flushes on every init.
    – Milo
    Apr 13, 2018 at 12:58
  • Absolutely, I've done exactly that. However, for some reason it destroyed the archive after updating the permalinks then, so I didn't update those again. Strange behavior.
    – N. M.
    Apr 13, 2018 at 14:37
  • The order of things is wrong somewhere. It’s not clear when your taxonomy is being registered, I don’t see it hooked to an action or what else may be going on in that class, but if rewrites disappear then those rules weren’t added before the flush.
    – Milo
    Apr 13, 2018 at 15:05

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.