Permalink for CPT without archive but including taxonomy returns 404 on all other pages and posts

Question

I’m trying to achieve the following permalink structure for a Diensten custom post type:
https://domain.com/%taxonomy%/%postname%/

With the code below I’ve achieved the desired permalink structure however it returns a 404 on all other posts and pages…

It has something to do with the fo_diensten_permalink function as it seems to target all posts and pages and not just the diensten custom post type. According to this link https://shibashake.com/wordpress-theme/custom-post-type-permalinks-part-2#conflict it can be resolved by adding a unique slug into the permalink structure. However when I do that it breaks the custom post type permalink structure. On the other hand, it solves the issue with the 404 for the other pages and posts.

This is the code that I’m using to register the Diensten post type:

function cptui_register_my_cpts() {

/**
 * Post Type: Diensten.
 */

$labels = [
    "name" => __( "Diensten", "custom-post-type-ui" ),
    "singular_name" => __( "Dienst", "custom-post-type-ui" ),
];

$args = [
    "label" => __( "Diensten", "custom-post-type-ui" ),
    "labels" => $labels,
    "description" => "",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => false,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => [ "slug" => "%type_dienst%", "with_front" => false, 'hierarchical' => false ],
    "query_var" => true,
    "supports" => [ "title", "editor", "thumbnail" ],
    "taxonomies" => [ "type_dienst" ],
];

register_post_type( "diensten", $args );
}

add_action( 'init', 'cptui_register_my_cpts' );

Registering the Type diensten taxonomy’

function cptui_register_my_taxes() {

/**
 * Taxonomy: Type dienst.
 */

$labels = [
    "name" => __( "Type dienst", "custom-post-type-ui" ),
    "singular_name" => __( "Type dienst", "custom-post-type-ui" ),
];

$args = [
    "label" => __( "Type dienst", "custom-post-type-ui" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => true,
    "show_admin_column" => true,
    "show_in_rest" => true,
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
        ];
register_taxonomy( "type_dienst", [ "diensten" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes' );

The rewrite function

// The post_link hook allows us to translate tags for regular post objects
add_filter('post_link', 'fo_diensten_permalink', 10, 3);
// The post_type_link hook allows us to translate tags for custom post type objects
add_filter('post_type_link', 'fo_diensten_permalink', 10, 3);

function fo_diensten_permalink($permalink, $post_id, $leavename) {
// If the permalink does not contain the %type_dienst% tag, then we don’t need to translate anything.
if (strpos($permalink, '%type_dienst%') === FALSE) return $permalink;

// Get post
$post = get_post($post_id);
if (!$post) return $permalink;

// Get the type diensten terms related to the current post object.
$terms = wp_get_object_terms($post->ID, 'type_dienst');

// Retrieve the slug value of the first diensten custom taxonomy object linked to the current post.
if ( !is_wp_error($terms) && !empty($terms) && is_object($terms[0]) )
  $taxonomy_slug = $terms[0]->slug;
// If no type_dienst terms are retrieved, then replace our type_dienst tag with the value "type_dienst"
else
  $taxonomy_slug = 'type_dienst';

// Replace the %type_dienst% tag with our custom taxonomy slug
return str_replace('%type_dienst%', $taxonomy_slug, $permalink);
}

Any help is much appreciated.

0
NielsPilon 2 months 0 Answers 5 views 0

Leave an answer