WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

closed as unclear what you're asking by kaiser Oct 21 '14 at 14:11

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the 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