• H Ford

    (@bastiensabatier)


    Hi,

    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/custom_post_name.
    I’ve got a taxonomy template (taxonomy.php) to display custom posts related to a term and it’s working well.
    So far so good.
    But permalinks are still like this : my-site/projects/custom_post_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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter H Ford

    (@bastiensabatier)

    I may transfer all my taxonomies as categories and it will work but I’ll come with a huge amount of categories and by the way, these taxonomies were specific to a custom post type… so now i would have the whole categories for both (posts and custom post types) which would be confusing, no ?
    please any help or advice ?

    Thread Starter H Ford

    (@bastiensabatier)

    Finally i came up with this :

    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'))
        {
          // last part in the 'category_name' should be the slug for the current category
          $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');

    Different permalinks are pointing well to the same single post (no 404) but WP keeps rewriting at least ‘my-site/projects/post_name’. Where may i have to look to force WP to keep both url ?
    cheers

    Thread Starter H Ford

    (@bastiensabatier)

    anyone ?

    Thread Starter H Ford

    (@bastiensabatier)

    thanks everyone i made it work 😉

    Hi,

    I’m having exactly the same problem where I need to display a custom post type under two different URLs based on which taxonomy they have selected from the navigation (clients/projects).

    I’ve been looking for a while for a solution with many attempts involving modifying rewrite rules etc. to no avail.

    How did you manage to solve it finally?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Multiple permalinks for a single post with multiple taxonomies’ is closed to new replies.