Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom taxonomy permalinks with parent/child relationships WordPress

Howdy folks,

I need to create a custom permalink structure based on a custom taxonomy. My taxonomy is called "location" and it will eventually list all countries (US, UK, FR, etc) as Parent Terms and each countries State or Territory as a Child Term. I plan to use these taxonomies to build intuitive location-based permalinks, like this:

http://example.com/us/virginia/post-name/
(us is Parent Term in Location taxonomy, virginia is Child term of us)

http://example.com/uk/perth/post-name/
(uk is Parent Term in Location taxonomy, perth is Child term of uk)

Etc, etc.

Here is the code I'm using in my theme's functions.php:


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

// looks for %location% in the Custom Permalinks settings
function shredtopia_spot_location_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%location%') === FALSE) return $permalink;

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

// Get taxonomy terms. Change location to your taxonomy slug
$terms = wp_get_object_terms($post->ID, 'location');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
else $taxonomy_slug = 'other';

// replaces %location% with the actual name of the taxonomy term
return str_replace('%location%', $taxonomy_slug, $permalink);
}


My code above only shows the parent term (the country) and not the child term (the state). As I mentioned above, I need it to do this:

http://example.com/us/virginia/post-name/
(us is Parent Term in Location taxonomy, virginia is Child term of us)

http://example.com/uk/perth/post-name/
(uk is Parent Term in Location taxonomy, perth is Child term of uk)

Also, for what it's worth, the [[LINK href="http://wordpress.org/extend/plugins/taxonomic-seo-permalinks/"]]Taxonomic SEO Permalinks plugin[[/LINK]] does provide greater flexibility if you prefer the plugin approach over hacking functions.php.

Thoughts? Really struggling with this. Could use some help from the WP Gods.

Answers (2)

2011-07-15

Erez S answers:

Well, you can use the above plugin like that:
/%location%/%state%/%postname%/

But you would have to change the states and country from child terms, to new term called "state".

2011-07-15

Utkarsh Kukreti answers:

Try this: (I haven't tested yet).

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

// looks for %location% in the Custom Permalinks settings
function shredtopia_spot_location_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%location%') === FALSE) return $permalink;

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

// Get taxonomy terms. Change location to your taxonomy slug
$terms = wp_get_object_terms($post->ID, 'location');
if (!is_wp_error($terms) && !empty($terms) && count($terms) == 2) {
if(!$terms[0]->parent) {
$taxonomy_slug = $terms[0]->slug . "/" . $terms[1]->slug;
} else {
$taxonomy_slug = $terms[1]->slug . "/" . $terms[0]->slug;
}
}
else $taxonomy_slug = 'other';

// replaces %location% with the actual name of the taxonomy term
return str_replace('%location%', $taxonomy_slug, $permalink);
}


Thad Allender comments:

@Utkarsh - Your approach gets close, but, it assumes that users will always add the Parent term first. In my use case, I'm building a user-generated directory of "spots". I can't assume users will add the parent term (country) before they add the child term (state). Ideally the code would look for the parent term, instead of just looking for the first term selected.

@Erez - Location is one taxonomy. There are a couple of reasons that I won't go into why adding Country as one taxonomy and State as another taxonomy isn't feasible.


Utkarsh Kukreti comments:

Please try the edited version.


Utkarsh Kukreti comments:

My updated code checks to see that the parent taxonomy is always added first. Let me know if it works.


Thad Allender comments:

Yep, that works @Utkarsh! Thanks!