0

I'm working on a Real Estate site using the following permalink format for properties:

http://www.site.com/property/1330-5th-avenue (property is a custom post type for listings)

How can I modify the permalink for these posts to

http://www.site.com/new-york/1330-5th-avenue (being new New York a term which is associated with custom taxonomy City)

I have been researching but I really need help. Is it even possible?

1 Answer 1

0

Add or replace this to your register_taxonomy (in functions.php):

'query_var' => 'city',
'rewrite' => true

Then append this to your functions.php

add_filter('post_link', 'city_permalink', 10, 3);
add_filter('post_type_link', 'city_permalink', 10, 3); 
function city_permalink($permalink, $post_id, $leavename) {
  if (strpos($permalink, '%city%') === FALSE) return $permalink;
  // Get post
  $post = get_post($post_id);
  if (!$post) return $permalink;
  // Get taxonomy terms
  $terms = wp_get_object_terms($post->ID, 'city');   
  if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
  else $taxonomy_slug = 'not-rated';
  return str_replace('%city%', $taxonomy_slug, $permalink);
}

Here you can find a more detailed description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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