WordPress.org

Ready to get started?Download WordPress

Forums

Paginate custom post foreach loop by taxonomy (13 posts)

  1. longley
    Member
    Posted 1 year ago #

    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.

    <?php
    
    $custom_terms = get_terms('issues');
    
    foreach($custom_terms as $custom_term) {
    	wp_reset_query();
    	$args = array(
    	'post_type' => 'news',
    	'posts_per_page' => '1',
    	'tax_query' => array(
    		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>'; ?>
    
    </div>
    
    <?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]

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    On what theme template do you want to do this?

  3. longley
    Member
    Posted 1 year ago #

    page.php

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  5. longley
    Member
    Posted 1 year ago #

    I would probably exclude them. So not show them.

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  7. longley
    Member
    Posted 1 year ago #

    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.

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    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: http://pastebin.com/DguHUyBp

    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

  9. longley
    Member
    Posted 1 year ago #

    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
    Excerpt

    Issue 2
    Post Title
    Excerpt

    Issue 3
    Post Title
    Excerpt

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

    Hope that makes sense.

  10. keesiemeijer
    moderator
    Posted 1 year ago #

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

  11. keesiemeijer
    moderator
    Posted 1 year ago #

    I think this will do it.
    Put this in your theme's functions.php: http://pastebin.com/AvVe7mc9
    And use this in your page template: http://pastebin.com/jr0PW6EC

    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';
  12. longley
    Member
    Posted 1 year ago #

    Works like a charm.

    Thank you for sparing your time to help with this.

  13. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem :-)
    I'm glad you've got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic