Force Rewrite CPT slug with CPTonomy attached to it
-
Hi there,
First off, great plugin, I love it!
I have a CPT, let’s call it ‘my-cpt.’ I’m force rewriting the permalink for this CPT to prepend some stuff. I have this working, but am unable to have the term from my CPTonomy, we’ll call this ‘my-cptonomy,’ (it’s hierarchical) work in the slug. My code:
add_filter('post_link', 'mycptonomy_permalink', 10, 3); add_filter('post_type_link', 'mycptonomy_permalink', 10, 3); function mycptonomy_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%mycptonomy%') === FALSE) return $permalink; // Get post $post = get_post($post_id); if (!$post) return $permalink; // Get taxonomy terms $terms = get_terms($post->ID, 'mycptonomy'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = ''; return str_replace('%mycptonomy%', $taxonomy_slug, $permalink); } add_action('init', 'force_mycpt_rewrite'); function force_mycpt_rewrite() { global $wp_rewrite; $wp_rewrite->add_permastruct('mycpt', 'prepend-stuff/%mycptonomy%/%postname%/', false, 1); add_rewrite_rule('mycpt/([0-9]{4})/(.+)/?$', 'index.php?mycpt=$matches[2]', 'top'); $wp_rewrite->flush_rules(); // !!! }
The above code works if I take the
%mycptonomy%
bit out (~line 23). However, when I add it in, the link redirects to the CPTonomy post page itself.Could you please let me know how I might accomplish adding my CPTonomy to a post slug?
Thanks in advance!
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Force Rewrite CPT slug with CPTonomy attached to it’ is closed to new replies.