I'm currently working on a website which is a collective name of 2 schools. That's why I've renamed the taxonomy categories to schools. So in every post of a custom post type I can select the right school/category.
Now I have 3 custom post types in this theme.
- Agenda
- Schoolgids (schoolguide)
- Vacatures (vacancies)
Everything looks like it's working good, but only the URL-structure is not the way I want it to be. The way I want it, is for example: domainname.com/schoolname/post-type/post-name. My custom post type(s) is/are looking like this:
Post type (agenda for example):
function custom_post_agenda() {
register_post_type( 'agenda',
array('labels' => array(
'name' => __('Agenda', 'raadhuiswp'),
'singular_name' => __('Agenda', 'raadhuiswp'),
'all_items' => __('Alle agendapunten', 'raadhuiswp'),
'add_new' => __('Nieuw agendapunt', 'raadhuiswp'),
'add_new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
'edit' => __( 'Wijzig', 'raadhuiswp' ),
'edit_item' => __('Wijzig agendapunt', 'raadhuiswp'),
'new_item' => __('Voeg nieuw agendapunt toe', 'raadhuiswp'),
'view_item' => __('Toon agendapunt', 'raadhuiswp'),
'search_items' => __('Zoeken naar agendapunten', 'raadhuiswp'),
'not_found' => __('Niks gevonden in de database.', 'raadhuiswp'),
'not_found_in_trash' => __('Niks gevonden en de prullenbak.', 'raadhuiswp'),
'parent_item_colon' => ''
),
'description' => __( 'Een post type voor agendapunten', 'raadhuiswp' ),
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_ui' => true,
'query_var' => true,
'menu_position' => 8,
'menu_icon' => 'dashicons-calendar-alt',
'rewrite' => array( 'slug' => '%school%/agenda',
'with_front' => true ),
'has_archive' => true,
'capability_type' => 'post',
'hierarchical' => false,
'show_in_rest' => true,
'supports' => array( 'title', 'author', 'thumbnail', 'excerpt', 'revisions', 'page-attributes')
)
);
register_taxonomy_for_object_type('category', 'agenda');
register_taxonomy_for_object_type('post_tag', 'agenda');
}
add_action( 'init', 'custom_post_agenda');
Now I've already managed to rewrite the slug for the post types with the following function:
Function:
function school_post_type_link($link, $post) {
if ($post->post_type == 'agenda') {
if ($school = get_the_terms($post->ID, 'category')) {
$link = str_replace('%school%', array_pop($school)->slug, $link);
return $link;
}
} elseif ($post->post_type == 'vacatures') {
if ($school = get_the_terms($post->ID, 'category')) {
$link = str_replace('%school%', array_pop($school)->slug, $link);
return $link;
}
} elseif ($post->post_type == 'schoolgids') {
if ($school = get_the_terms($post->ID, 'category')) {
$link = str_replace('%school%', array_pop($school)->slug, $link);
return $link;
}
} else {
return $link;
}
}
add_filter('post_type_link', 'school_post_type_link', 1, 3);
This way, I replace the '%school%' for the for the category name. And it seem to work:
So what I'm basicly doing is, checking if the post type is one of my custom post types, and if so, I replace the slug of it.
Now when I click view page, I keep getting the page not found (404) page. I have no idea why this is, because I can edit the post in WordPress itself without any problems.
Any idea what could cause this? Thanks in advance!