Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I think thats a quick one:

I've got:

  1. Custom post type: Food
  2. Custom taxonomy (Registered to Food): Fruits
  3. Terms in Fruits: Apple, Orange, Cherry

If I type in example.com/food?fruits=Apple,Cherry, I get all posts in Fruits with the terms Apple and Cherry, thats great!

But I would like to type in example.com/food/fruits/Apple,Cherry or example.com/food/Apple,Cherry to get the same result.

I´ve tried different permalink and rewrite plugins related to custom posts but nothing helped.

Thank you,

David

share|improve this question
    
Similar implementation can be found here. wordpress.stackexchange.com/questions/57493/… –  Saurabh Goel Jul 6 '12 at 6:48

1 Answer 1

up vote 2 down vote accepted

You have to build up the link structure by using filters post_link and post_type_link:

add_filter('post_link', 'territorio_permalink', 10, 3);
add_filter('post_type_link', 'territorio_permalink', 10, 3);

function territorio_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%territorio%') === FALSE) return $permalink;

// Get post
$post = get_post($post_id);
if (!$post) return $permalink;

// Get taxonomy terms
$terms = wp_get_object_terms($post->ID, 'territorio','orderby=term_order');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
$taxonomy_slug = $terms[0]->slug.'/'.$terms[1]->slug; //build here
else $taxonomy_slug = 'not-yet';

return str_replace('%territorio%', $taxonomy_slug, $permalink);
}

Where hotel is the post type and territorio is a hierarchical taxonomy.

In post type creation use:

'rewrite' => array( 'slug' => 'anything-you-want/%territorio%','with_front' => false),

Note: if you want deeper links, your build should go deeper:

$taxonomy_slug = $terms[0]->slug.'/'.$terms[1]->slug.'/'.$terms[2]->slug;
share|improve this answer
    
Thank you, that works pretty well! But I have another custom post type which is using the same taxonomy and I always get posts of both categories. –  dee Jun 15 '12 at 20:00
    
not categories but custom post types –  dee Jun 15 '12 at 20:07
    
I get the same posts by typing example.com/food/fruits/Apple,Cherry or example.com/meal/fruits/Apple,Cherry, I think wordpress no more care about the /food or /meal. –  dee Jun 15 '12 at 20:12
    
I'm not into sharing taxonomies, as we talked here: wordpress.stackexchange.com/questions/55055/… Is ok same values in two different taxos but same taxos turn things a mess when filtering. –  Diana Jun 15 '12 at 20:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.