Support » Fixing WordPress » Paginate custom post foreach loop by taxonomy

  • I am trying to create an archive list showing a single post from a custom post type sorted by the taxonomy slug and then paginate the list after 5 results. I have successfully produced the list but have no idea how to implement pagination. I would be grateful for any help.

    $custom_terms = get_terms('issues');
    foreach($custom_terms as $custom_term) {
    	$args = array(
    	'post_type' => 'news',
    	'posts_per_page' => '1',
    	'tax_query' => array(
    		'taxonomy' => 'issues',
    		'field' => 'slug',
    		'terms' => $custom_term->slug,
    $loop = new WP_Query($args);
    if($loop->have_posts()) {
    echo '<h2>Edition '.$custom_term->name.'</h2>';?>
    <h4 class="h6"><?php echo date('l jS F Y'); ?></h4>
    <?php while($loop->have_posts()) : $loop->the_post(); ?>
    <div class="newsitem mb20">
    <a>"><?php the_title(); ?></a>
    <?php if (has_post_thumbnail()) :?>
    <div class="post-imagesmall"><a>"><?php the_post_thumbnail(otherstory); ?></a></div>
    <?php else : ?>
    <div class="post-imagesmall"><a>"><img src="/wp-content/upload/thepress-defaultsmall.jpg" /></a></div>
    <?php endif ?>
    <?php $myExcerptLength=60; the_excerpt(); ?>
    <a>">Read More ยป</a>
    <?php echo '<a>slug, 'issues').'">More Articles from this edition</a>'; ?>
    <?php endwhile;

    [moderated: please ensure that your code is enclosed in backticks (`) or use the code button. Your code might be broken by the forum’s software]

Viewing 12 replies - 1 through 12 (of 12 total)
  • Moderator keesiemeijer


    On what theme template do you want to do this?


    Moderator keesiemeijer


    Ah that’s good. You can’t use the normal pagination functions so you have to work with the ‘offset‘ parameter on paged pages. Also because all terms will have a different post count would you like to show that there are no more posts for that term on paginated pages?

    Edition cats
    Pictures of kittens (post title)
    Edition dogs
    Sorry, no more dogs posts
    Edition horses:
    Tips on buying a horse (post title)

    Or would you like to not show them?

    Edition cats
    kittens (post title)
    Edition horses
    Tips on buying a horse (post title)

    I will See if I can conjure something up.

    I would probably exclude them. So not show them.

    Moderator keesiemeijer


    A few more questions:

    Is the “issues” taxonomy hierarchical like categories.
    if so, are all terms in the “issues” taxonomy top level terms or are there child terms as well? Do you want to show posts from child terms (if there are any) or not?

    It’s more difficult than I thought.

    All posts will be from a top level term within the issue taxonomy. I am not planning on having any children.

    Thanks for your help with this.

    Moderator keesiemeijer


    Try it with this in your theme’s functions.php:

    // get the (custom post type) post count for a term in a (custom) taxonomy
    function get_term_post_type_count( $term_id, $tax = 'category', $post_type = 'post' ){
    		global $wpdb;
    		$query = "
    		  SELECT COUNT(ID) FROM $wpdb->posts
    		  LEFT JOIN $wpdb->term_relationships ON
    		  ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    		  LEFT JOIN $wpdb->term_taxonomy ON
    		  ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    		  WHERE $wpdb->posts.post_status = 'publish'
    		  AND $wpdb->posts.post_type = '" . $post_type . "'
    		  AND $wpdb->term_taxonomy.taxonomy = '" . $tax . "'
    		  AND $wpdb->term_taxonomy.parent = 0
    		  AND $wpdb->term_taxonomy.term_id = '" . $term_id . "'
    		return $wpdb->get_var($query);

    Here is an example of a page template that shows posts for every term and pagination functions on the bottom:

    Put your own code from (inside) your loop after this:

    <!-- put your loop code here -->

    Right now it only shows the post title. I didn’t do much testing so you have to do that yourself

    change $posts_per_page = 1; // how many posts per term for how many posts you want to show per term

    Hi keesiemeijer,

    I absolutely love what you have produced and will definitely keep it for future projects.

    Unfortunately it does not do what I need it to. That is probably my fault for not giving enough detail.

    This is what I need sort of.

    terms per page = 3 (for this example)

    Issue 1 <–Term
    Post Title

    Issue 2
    Post Title

    Issue 3
    Post Title

    >>Pagination Here<< (to Issue 4, 5 and 6 Post title and Excerpt)

    Hope that makes sense.

    Moderator keesiemeijer


    Ah, that makes total sense and can be done. I don’t have time right now but I’ll look into it tommorow.

    Moderator keesiemeijer


    I think this will do it.
    Put this in your theme’s functions.php:
    And use this in your page template:

    Put your own code from (inside) your loop after this:

    <!-- put your loop code here -->

    and edit these variables:

    // edit these variables
    $posts_per_page = 1; // how many posts per term
    $terms_per_page = 3; // how many terms to loop through
    $post_type = 'news';
    $taxonomy = 'issues';

    Works like a charm.

    Thank you for sparing your time to help with this.

    Moderator keesiemeijer


    No problem ๐Ÿ™‚
    I’m glad you’ve got it resolved.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Paginate custom post foreach loop by taxonomy’ is closed to new replies.