How can I get a paginated list of custom taxonomy tags with posts?

Question

I have a cpt (item), in which each post is assigned a single custom taxonomy tag (item_tags).

I want to list all tags, with description and linked titles of posts so-tagged.

I’ve had previous help here, and have found other code to list the tags and posts, but I can’t paginate it – because they each use ‘get_terms’.

The two existing options are (and, as pasted, have extra html formatting for my requirements):

$terms = get_terms('item_tags');
foreach ($terms as $term):
    ?>

    <?php echo apply_filters('the_content', $term->description) . '</dt>'; ?>
    <?php
    $posts = get_posts(array(
        'post_type' => 'item',
        'taxonomy' => $term->taxonomy,
        'term' => $term->slug,
        'posts_per_page' => -1,
        'order' => 'ASC',
    ));
    foreach ($posts as $post): // begin cycle through posts of this taxonmy
        setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
        ?>
        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    <?php endforeach;
    echo '</dl>' . "n" . '<hr />' . "nn"; ?>
<?php endforeach; ?>

And:

global $post;
$terms = get_terms(array(
    'taxonomy' => 'item_tags',
    'hide_empty' => false,
));
if (!empty($terms) && !is_wp_error($terms)) {
    foreach ($terms as $term) {
        echo apply_filters('the_content', $term->description);
        echo '</dt>' . "n";
        $items = get_posts(array(
            'post_type' => 'item',
            'tax_query' => array(
                array(
                    'taxonomy' => 'item_tags',
                    'terms' => $term->term_id,
                ),
            ),
            'posts_per_page' => -1,
            'order' => 'ASC',
        ));
        foreach ($items as $post) {
            setup_postdata($post);
            ?>
            <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
            <?php
        }
        echo '</dl>' . "n" . '<hr />' . "nn";
    }
    wp_reset_postdata();
}

FURTHER UPDATE:

Although I still want to get the solution from Max Yudin working, I’ve an interim alternative using code Max and also from https://wordpress.stackexchange.com/a/22101/125227 (suggested by DHL17).

I’ll include it here, as it may be useful to someone.

// Terms are formatted as ul.
// Posts are nested ul.  
// Has "n" for line-break in source-code.

$args = array( 'hide_empty' => 1 );

$page = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;

// Number of tags to show per-page.
$per_page = 13;
$offset = ( $page-1 ) * $per_page;
$args = array( 'number' => $per_page, 'offset' => $offset,     'hide_empty' => 1 );


$taxonomy = 'item_tags';
$tax_terms = get_terms( $taxonomy, $args );

// Open ul for terms.
echo '<ul>' . "nn";

// Cycle through taxonomy terms, and open sub-ul for posts.
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>' . "n" . '<ul>' . "n";

$post_type = 'item';
$taxonomy = 'item_tags';

// Cycle through posts for this term.
$items = get_posts( array (
'post_type'   => $post_type,
'tax_query'   => array(
array(
'taxonomy' => $taxonomy,
'terms'    => $tax_term->term_id,
), ),

// Set variables for posts-per-term
'numberposts' => -1,
'orderby' => 'date',
'order' => 'asc',
) );

global $post;

foreach ( $items as $item ) {

// Assign $item to global $post.
$post = $item;

// Post info to display.
setup_postdata( $post );
echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>' . "n";
}

// Close sub-ul for posts.
echo '</ul>' . "nn";

// Reset.
wp_reset_postdata();
}

// Close ul for terms.
echo '</ul>' . "nn";


// Pagination.
$total_terms = wp_count_terms( 'item_tags' );
$pages = ceil($total_terms/$per_page);

if( $pages > 1 ):
echo '<ul>' . "n";

for ($pagecount=1; $pagecount <= $pages; $pagecount++):
echo '<li><a href="'.get_permalink() . 'page/' . $pagecount . '/">' .     $pagecount . '</a></li>' . "n";
endfor;
echo '</ul>';
endif;
0
glvr 2 months 0 Answers 10 views 0

Leave an answer