Post Link Filter Help

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.

Read More
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.

Related posts

Leave a Reply