How to get term link that crosses different custom post types?

This code display all posts of all categories of genre taxonomy for custom post type book. Now, For different custom post type (author, product ) you have to change Custom Post Type Name inside $arg of WP_Query(). You will get term link using get_term_link($catterm) function or you can use also get_the_term_list().

 $args = array(
            'number'     => $number,
            'hide_empty' => $hide_empty,
            'include'    => $ids
        );

        $custom_categories = get_terms( 'genre ', $args );

        foreach ( $custom_categories as $catterm){

            $arg = Array( 
            'post_type' => 'book',
            'posts_per_page' => '-1',
            'post_status' => 'publish',
            'tax_query' => Array( Array ( 
            'taxonomy' => 'genre ' ,
            'terms' => $catterm->term_id
            )) );

        $loop = new WP_Query( $arg ); 
        global $post;
        while ( $loop->have_posts() ) : $loop->the_post();
        ?>

        <div class="gallery-content">
        <div class="entry-content">

        <?php 
         echo '<li>' . get_the_title() . '</li>';
         echo '<li><a href="'.get_term_link($catterm).'">'.$catterm->name.'</a></li>';   
        ?>  

        </div>
        </div>

       <?php endwhile;
        }   
        ?>

Leave a Comment