I created 1 custom post type (projects) with 2 taxonomies (clients / fields) to filter the results (like categories).
At the end, it should be like this where project_name is the same custom post displayed in single-projects.php :
- my-site/projects/
- my-site/projects/project_name
- my-site/projects/fields/
- my-site/projects/fields/project_name
- my-site/projects/clients/
- my-site/projects/clients/project_name
Like this, based on the re-written url, i'll be able to easily display the current term selected and navigate post per post (in single-projects.php) within this specific term .
I'm using an archive to display all custom posts: archive-projects.php.
The custom posts permalinks are like this: my-site/projects/project_name
I've got a taxonomy template to display custom posts related to a term and it's working well: taxonomy.php
So far so good.
But permalinks are still like this : my-site/projects/project_name.
And i'd like to give them a different permalink based on the current taxonomy and get a different URL as written above.
I already found this and it's working well in a standard case (a blog with few categories for example), but i got stuck when i'm trying to adapt to taxonomies. I can create the new url but can't give it to the permalink post.
Any help or advice? thanks
[UPDATE]
Here is what i managed to do so far :
function multiple_taxonomy_post_link($CPT_url = ){
// check permalink structure for the required construct; /%category%/%postname%/
if (strrpos(get_option('permalink_structure'), '%category%/%postname%') !== false){
// get the current post
global $post, $wp_query;
// prepare variables for use below
$post_id = $term_id = 0;
$new_CPT_url = '';
// for taxonomies
if (is_tax())
{
// remember current category and post
$term_obj = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$term_id = $term_obj->term_id;
$post_id = $post->ID;
// add the post slug to the current url
$new_CPT_url = $_SERVER['REQUEST_URI'] . $post->post_name;
}
else if (is_singular('projects'))
{
$term_object = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
// remember current category and post
$post_id = $wp_query->post->ID;
if ($term_object) $term_id = $term_object->term_id;
// replace the slug of the post being viewed by the slug of $post
$new_CPT_url = $_SERVER['REQUEST_URI'].$post->post_name;
}
if ($post_id > 0 && $term_id > 0 && !empty($new_CPT_url))
{
$current_tax = get_query_var( 'taxonomy' );
$terms = get_the_terms( $post->ID, $current_tax );
// make sure categories match!
foreach($terms as $term)
{
if ($term->term_id == $term_id)
{
$CPT_url = $new_CPT_url;
break;
}
}
}
}
// always return an url!
return $CPT_url;
}
add_filter('post_type_link', 'multiple_taxonomy_post_link');
it displays well the new permalinks on taxonomy template and redirections are pointing to the desired post but WP keeps writing the single post url as my-site/projects/project_name. Is it because the targeted post is a custom post type ?