Forum Replies Created

Viewing 1 replies (of 1 total)
  • Simone

    (@webdesignsimone)

    Hi,
    I had the same problem and i solved it with the new filter cptp_post_link_term to get the primary term by yoast plugin, thanks to the developers!

    I have a Custom Post Type called ‘percorsi’ with 2 custom term ‘cat_sport’ and ‘cat_luoghi’. The setting in CPTP is: /%cat_luoghi%/%cat_sport%/%postname%/
    The resulted URL is correctly it/percorsi/location/sport/title/ but by default the first taxonomy in alphabetical order is taken.

    Since I haven’t found the documentation I ask to the plugin authors if the following code is correct or there is a better way:

    function cptp_term_luogo( $term_obj, $terms, $taxonomy, $post ){
    
        $post_id = $post->ID;
        
        if(get_post_type($post_id) == 'percorsi' && !is_post_type_archive('percorsi') ){
            //SPORT PRIMARIO
            $primary_sport = new WPSEO_Primary_Term('cat_sport', $post_id);
            $primary_sport = $primary_sport->get_primary_term();
            if($primary_sport):
                $sport = get_term($primary_sport);
            endif;
            //LUOGO PRIMARIO
            $primary_luogo = new WPSEO_Primary_Term('cat_luoghi', $post_id);
            $primary_luogo = $primary_luogo->get_primary_term();
            if($primary_luogo):
                $luogo = get_term($primary_luogo);
            endif;
    
            if($taxonomy == 'cat_sport'):
                $term_obj = $sport;
            endif;
    
            if($taxonomy == 'cat_luoghi'):
                $term_obj = $luogo;
            endif;
    
            return $term_obj;  
    
        } else {
    
            return $term_obj;  
    
        }
    }
    add_filter( 'cptp_post_link_term', 'cptp_term_luogo', 10, 4);
Viewing 1 replies (of 1 total)