Skip to main content
Added 'orderby=term_id', without it, the terms jumps due to alphabetical default
Source Link
Diana
  • 1.2k
  • 1
  • 13
  • 31

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;

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');
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;

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;
Source Link
Diana
  • 1.2k
  • 1
  • 13
  • 31

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');
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;