Techballs
Techballs

Reputation: 11

WordPress Custom Post Type change for permalink showing 404

I have created a custom post type post_event_type and have set rewrite to true when registering.

Within this post type, there are categories under the taxonomy of event_Category with rewrite set to true

The url then resolves for this post to URL/post_event_type/postname

There are three categories under the taxonomy event_Category such as cinema, dance, music.

I want the url to resolve to the page as such URL/dance/postname so 'dance' would be the taxonomy category and post name would be the post residing underneath this category

If I change the register post type rewrite to 'events' they would all resolve to URL/events/postname

I have then tried the following coding to try and seperate these out. The URLs work in the way I wan them to, however they then go to a 404 page.

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');

function mmp_rewrite_rules($rules) {
$newRules  = array();
$newRules['events/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?vc_guide_creator=$matches[4]'; 
$newRules['events/(.+)/?$']                = 'index.php?event_Category=$matches[1]'; 

return array_merge($newRules, $rules);
}

global $wp_rewrite;
$structure = '/%events%/';
$wp_rewrite->add_rewrite_tag("%events%", '([^/]+)', "post_event_type=");
$wp_rewrite->add_permastruct('post_event_type', $structure, false);

function filter_post_type_link($link, $post)
{
if ($post->post_type != 'post_event_type')
    return $link;

if ($cats = get_the_terms($post->ID, 'event_Category')){
    $link = str_replace('post_event_type', array_pop($cats)->slug, $link); 

}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

Can anyone help please?

Upvotes: 0

Views: 510

Answers (1)

Salam El-Banna
Salam El-Banna

Reputation: 3870

Have you tried to update your permalinks.

Go to Settings-> Permalinks and click Save changes button in the bottom without changing any of the settings, this will simply update the permalinks table.

Upvotes: 1

Related Questions