0

I have looked around noth in StackOverflow and WP.SE.

Like many before me, I just want this structure

mysite.com/custom-post-type/custom-taxonomy-term/post-name

Or, in other words,

mysite.com/publication/book/post-name (single post)
mysite.com/publication/book/          (all posts in the term 'book')
mysite.com/publication/               (all posts in this publication CPT)

I followed this instruction by TheDeadMedic for making custom post type with custom taxonomy permalink, and another one by Matthew Boynes to fix the pagination issue.

All works well, except... everytime I open the single post, it returns the 404 page.

I have tried opening the Permalink page and setting flush_rewrite_rules() to no avail. I still can't open the single page.

The only fix that works is if I change the slug in my custom taxonomy to its original form.

So instead of publication (the rewrite slug for this tax, in accordance to my custom post type name), I would have the default type (the default slug for this tax). Which would defeat the purpose.

Here is my code.

Taxonomy.

    function cptui_register_my_taxes_type() {

/**
 * Taxonomy: Types.
 */

$labels = array(
    "name" => __( 'Types', 'mysite' ),
    "singular_name" => __( 'Type', 'mysite' ),
    "menu_name" => __( 'Types', 'mysite' ),
    "all_items" => __( 'All Types', 'mysite' ),
    "edit_item" => __( 'Edit Types', 'mysite' ),
    "view_item" => __( 'View Type', 'mysite' ),
    "update_item" => __( 'Update Type Name', 'mysite' ),
    "add_new_item" => __( 'Add New Type', 'mysite' ),
    "new_item_name" => __( 'New Type Name', 'mysite' ),
    "parent_item" => __( 'Parent Type', 'mysite' ),
    "parent_item_colon" => __( 'Parent Type:', 'mysite' ),
    "search_items" => __( 'Search Types', 'mysite' ),
    "popular_items" => __( 'Popular Types', 'mysite' ),
    "separate_items_with_commas" => __( 'Separate Types with commas', 'mysite' ),
    "add_or_remove_items" => __( 'Add or remove Types', 'mysite' ),
    "choose_from_most_used" => __( 'Choose from the most used Types', 'mysite' ),
    "not_found" => __( 'No Types found', 'mysite' ),
    "no_terms" => __( 'No types', 'mysite' ),
    "items_list_navigation" => __( 'Types list navigation', 'mysite' ),
    "items_list" => __( 'Types list', 'mysite' ),
);

$args = array(
    "label" => __( 'Types', 'mysite' ),
    "labels" => $labels,
    "public" => true,
    "hierarchical" => true,
    "label" => "Types",
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => array( 'slug' => 'publication', 'with_front' => true,  'hierarchical' => true, ),
    "show_admin_column" => true,
    "show_in_rest" => false,
    "rest_base" => "",
    "show_in_quick_edit" => true,
);
register_taxonomy( "tyoe", array( "publication" ), $args );
}

add_action( 'init', 'cptui_register_my_taxes_type' );

Custom Post Type

function cptui_register_my_cpts_publication() {

    /**
     * Post Type: Publication.
     */

    $labels = array(
        "name" => __( 'Publication', 'mysite' ),
        "singular_name" => __( 'Publication', 'mysite' ),
        "menu_name" => __( 'Publication', 'mysite' ),
        "all_items" => __( 'All Publication', 'mysite' ),
        "add_new" => __( 'Add New', 'mysite' ),
        "add_new_item" => __( 'Add New Publication', 'mysite' ),
        "edit_item" => __( 'Edit Publication', 'mysite' ),
        "new_item" => __( 'New Publication', 'mysite' ),
        "view_item" => __( 'View Publication', 'mysite' ),
        "view_items" => __( 'View Publications', 'mysite' ),
        "search_items" => __( 'Search Publications', 'mysite' ),
        "not_found" => __( 'No publication found', 'mysite' ),
        "not_found_in_trash" => __( 'No trashed publications', 'mysite' ),
        "parent_item_colon" => __( 'Parent Publication:', 'mysite' ),
        "featured_image" => __( 'Cover or illustration', 'mysite' ),
        "set_featured_image" => __( 'Set cover or illustration for this publication', 'mysite' ),
        "remove_featured_image" => __( 'Remove cover or illustration for this publication', 'mysite' ),
        "use_featured_image" => __( 'Use as cover or illustration for this publication', 'mysite' ),
        "archives" => __( 'Publication archives', 'mysite' ),
        "insert_into_item" => __( 'Insert into publication info', 'mysite' ),
        "uploaded_to_this_item" => __( 'Uploaded to publication info', 'mysite' ),
        "filter_items_list" => __( 'Filter publication', 'mysite' ),
        "items_list_navigation" => __( 'Publication list navigation', 'mysite' ),
        "items_list" => __( 'Publication list', 'mysite' ),
        "attributes" => __( 'Publication Attributes', 'mysite' ),
        "parent_item_colon" => __( 'Parent Publication:', 'mysite' ),
    );

    $args = array(
        "label" => __( 'Publication', 'mysite' ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "publication/%type%", "with_front" => false ),
        "query_var" => true,
        "menu_position" => 6,
        "menu_icon" => "dashicons-format-aside",
        "supports" => array( "title", "editor", "thumbnail", "comments", "revisions" ),
        "taxonomies" => array( "type" ),
    );

    register_post_type( "publication", $args );
}

add_action( 'init', 'cptui_register_my_cpts_publication' );

Setting up the permalink

function change_permalink_publication( $link, $post ) {
    if ( $post->post_type === 'publication' ) {
        if ( $terms = get_the_terms( $post->ID, 'type' ) )
            $link = str_replace( '%type%', current( $terms )->slug, $link );
    }

    return $link;
}

add_filter( 'post_type_link', 'change_permalink_publication', 10, 2 );


add_action( 'generate_rewrite_rules', 'change_permalink_publication_pagination' );
function change_permalink_publication_pagination( $wp_rewrite ) {
    unset($wp_rewrite->rules['publication/([^/]+)/page/?([0-9]{1,})/?$']);
    $wp_rewrite->rules = array(
        'publication/?$' => $wp_rewrite->index . '?post_type=publication',
        'publication/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=publication&paged=' . $wp_rewrite->preg_index( 1 ),
        'publication/([^/]+)/page/?([0-9]{1,})/?$' => $wp_rewrite->index . '?type=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
    ) + $wp_rewrite->rules;
}

Is there a way to fix this?

4
  • if the questions you linked work, and your version doesn't, there would seem to be some difference, however most of the code you're using is not shown here, so it's difficult to say what that is.
    – Milo
    Commented Jul 5, 2017 at 1:18
  • @Milo edited, added the relevant CPT, custom taxonomy, and permalink setting.
    – deathlock
    Commented Jul 5, 2017 at 2:13
  • 1
    I assume "tyoe" in register_taxonomy is a typo? The problem is "hierarchical" => true in your taxonomy. Those rewrite rules are capturing the request for single publication posts, it's looking for a type term matching your post name. You can fix it immediately if you just set "hierarchical" => false. If you really want a hierarchical taxonomy, it'll get a little more complicated.
    – Milo
    Commented Jul 5, 2017 at 3:11
  • @Milo: Ah yes, that's a typo (had to translate it from my language to make more sense). By the way, setting the hierarchical to false fixes it for now. Thanks! Would be nice if there's a way to allow hierarchical taxonomy though. :)
    – deathlock
    Commented Jul 5, 2017 at 8:52

0

Your Answer

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