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 Am probably being a complete tool here but I am trying to change the permalink on my custom post type to a particular category child. The code in essence works but I cannot for the life of me figure out how to replace the wildcard for the custom taxonomy.

I have created the taxonomy product_cat and set rewrite to true. I am probably doing this all wrong or what I want is not achievable but thought I would ask you all anyway.

add_filter('post_link', 'product_cat_permalink', 10, 4);
add_filter('post_type_link', 'product_cat_permalink', 10, 4);

function product_cat_permalink($permalink, $post_id, $leavename) {

if (strpos($permalink, '%product_cat%') === FALSE) return $permalink;

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

$terms = wp_get_object_terms($post->ID, 'product_cat');  
$term_to_use = null;

// -- Loop The Terms
foreach($terms as $term) {
    if($term->parent == 17) {
        $term_to_use = $term;

if (!is_wp_error($terms) && !empty($terms) && is_object($term_to_use)) {
    $taxonomy_slug = $term_to_use->slug;
    return str_replace('%product_cat%', $taxonomy_slug, $permalink);
} else {
    return $permalink;

Thanks in advance.

share|improve this question
can you also post the code you use to handle your custom post type rewrite rules ? Where you are defining %product_cat% ? –  thomascharbit Jul 7 '13 at 15:40
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer


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

Browse other questions tagged or ask your own question.