3


I'm using Custom Post Template which I linked to Magic Fields to create custom post type named Company.
I also created custom taxonomy named City. City is hierarchical (like categories), and every Company has one city selected.

For example:
I have a company with post title Microsoft and City taxonomy Redmond selected. I want my permalinks to look like this:
http://www.mysite.com/Redmond/Microsoft

Just like when you have a post category and wordpress prepends first category selected to your permalink.
Can it be done?

ttkalec
  • 741
  • 1
  • 6
  • 26

2 Answers2

1

I've found solution to this problem. Here is the code:

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

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

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

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

return str_replace('%mba_courses%', $taxonomy_slug, $permalink);
}
gearsdigital
  • 13,915
  • 6
  • 44
  • 73
ttkalec
  • 741
  • 1
  • 6
  • 26
0

I haven't tried the plugin combination you mentioned but "normally", the permalink you want can only be achieved with category listings OR pages. What you want is something like: http://blogurl.com/categoryname/blogpostname And this doesn't happen in wordpress.

Your options are: http://blogurl.com/parentpage/childpage or http://blogurl.com/parentcategory/childcategory

Weptile
  • 104
  • 2
  • 9
  • I thought that taxonomies would somehow have similar funcionality as categories (as it derives from the same thing) :/ – ttkalec Feb 10 '11 at 10:05