Modifying permalink of a custom post type results in 404

I created a new custom post type “cpt_bobcat” and I need to alter its permalink. What I need is quite simple but with the plugin CPT UI it’s impossible to get what I want : /{post_type_name}/{category_term}/{post_name}. Right now, the permalink is superb, it has changed for every cpt_bobcat’s post (i.e. {my_website}/manufacturier_bob/bobcat-cat-1/test-product-bobcat1123/). However, when I click on the permalink from WP dashboard, I get a 404 error. Everything has been coded in functions.php… any tips on what is going wrong?

<?php

if(!function_exists(‘create_bobcat’)) {
function create_bobcat() {

$labels = array(
‘name’ => _x( ‘Bobcat’, ‘Post Type General Name’),
‘singular_name’ => _x( ‘Bobcat’, ‘Post Type Singular Name’),
‘menu_name’ => __( ‘Bobcat’),
‘parent_item_colon’ => __( ‘Parent Bobcat’),
‘all_items’ => __( ‘All Bobcat’s product’),
‘view_item’ => __( ‘View Bobcat’s product’),
‘add_new_item’ => __( ‘Add New Bobcat’s product’),
‘add_new’ => __( ‘Add New’),
‘edit_item’ => __( ‘Edit Bobcat’s product’),
‘update_item’ => __( ‘Update Bobcat’s product’),
‘search_items’ => __( ‘Search Bobcat’s product’),
‘not_found’ => __( ‘Not Found’),
‘not_found_in_trash’ => __( ‘Not found in Trash’),
);

$args = array(
‘label’ => __( ‘bobcat’),
‘description’ => __( ‘Bobcat news and reviews’),
‘labels’ => $labels,
‘supports’ => array( ‘title’, ‘editor’, ‘excerpt’, ‘author’, ‘thumbnail’, ‘comments’, ‘revisions’, ‘custom-fields’, ),
‘taxonomies’ => array( ‘tax_bobcat’ ),
‘hierarchical’ => false,
‘public’ => true,
‘show_ui’ => true,
‘show_in_menu’ => true,
‘show_in_nav_menus’ => true,
‘show_in_admin_bar’ => true,
‘menu_position’ => 5,
‘query_var’ => true,
‘can_export’ => true,
‘has_archive’ => true,
‘exclude_from_search’ => false,
‘publicly_queryable’ => true,
‘capability_type’ => ‘page’,
‘rewrite’ => array(‘slug’ => ‘manufacturier_bob’, ‘with_front’ => true),
);

// Registering your Custom Post Type
register_post_type( ‘cpt_bobcat’, $args );
}
}
add_action( ‘init’, ‘create_bobcat’ );

// add our new permastruct to the rewrite rules
add_filter( ‘cpt_bobcat_rewrite_rules’, ‘add_permastructtt’ );

function add_permastructtt( $rules ) {
global $wp_rewrite;

// set your desired permalink structure here
$struct = ‘/%manufacturier%/%category%/%postname%/’;

// use the WP rewrite rule generating function
$rules = $wp_rewrite->generate_rewrite_rules(
$struct, // the permalink structure
EP_PERMALINK, // Endpoint mask: adds rewrite rules for single post endpoints like comments pages etc…
false, // Paged: add rewrite rules for paging eg. for archives (not needed here)
true, // Feed: add rewrite rules for feed endpoints
true, // For comments: whether the feed rules should be for post comments – on a singular page adds endpoints for comments feed
false, // Walk directories: whether to generate rules for each segment of the permastruct delimited by ‘/’. Always set to false otherwise custom rewrite rules will be too greedy, they appear at the top of the rules
true // Add custom endpoints
);

return $rules;
}

// parse the generated links
add_filter( ‘post_type_link’, ‘custom_post_permalink’, 10, 4 );

function custom_post_permalink( $permalink, $post, $leavename, $sample ) {

if($post->post_type == ‘cpt_bobcat’ && get_option(‘permalink_structure’)) {

// var_dump($permalink);
// var_dump($post);
// var_dump($leavename);
// var_dump($sample);

$struct = ‘/%manufacturier%/%category%/%postname%/’;

$manufacturier = ”;
$category = ”;

$rewritecodes = array(
‘%manufacturier%’,
‘%category%’,
‘%postname%’
);

// manufacturier
switch($post->post_type) {
case ‘cpt_bobcat’:
$manufacturier = ‘manufacturier_bob’;
break;
}

// category
$terms = get_the_terms($post->ID, ‘tax_bobcat’);
$category = $terms[0]->slug;

$replacements = array(
$manufacturier,
$category,
$post->post_name
);

// replace permalink’s segment
$permalink = home_url( str_replace( $rewritecodes, $replacements, $struct ) );
$permalink = user_trailingslashit($permalink, ‘single’);

}

return $permalink;
}

Read more here:: Modifying permalink of a custom post type results in 404

Leave a Reply

Your email address will not be published. Required fields are marked *