• 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!

    https://www.ads-software.com/plugins/cpt-onomies/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter dornstudio

    (@dornstudio)

    Hello again,

    I worked on this a bit and found that if I change my permalink text from %mycptonomy% to something that doesn’t match the CPT name exactly (like %mycptonomy_slug%) then I am able to view the CPT with the correct slug. However, it shows a 404. My updated 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_slug%') === 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_slug%', $taxonomy_slug, $permalink);
    } 
    
    add_action('init', 'force_mycpt_rewrite');
    
    function force_mycpt_rewrite() {
    	global $wp_rewrite;
    	$wp_rewrite->add_permastruct('mycpt', 'prepend-stuff/%mycptonomy_slug%/%postname%/', false, 1);
    	add_rewrite_rule('mycpt/([0-9]{4})/(.+)/?$', 'index.php?mycpt=$matches[2]', 'top');
    	$wp_rewrite->flush_rules(); // !!!
    }

    Hopefully this is getting somewhat close and someone with a fresh set of eyes can point me in the right direction / help me with the last little bit.

    Thanks in advance!

    Thread Starter dornstudio

    (@dornstudio)

    Also, weirdly, when I add my permalink slug later in the string, it does work without a 404, for example:

    $wp_rewrite->add_permastruct('mycpt', 'prepend-stuff/%postname%/%mycptonomy_slug%/', false, 1);

    I have the same issue. Have you found a solution in the mean while ?

    Instead of

    $terms = get_terms($post->ID, 'mycptonomy');

    Use

    $terms = get_the_terms( $post->ID, 'mycptonomy' );

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.