Paginate for a simple loop

I’ve got this code which simply displays all the posts for a particular author:

<?php
    $all_active_tasks = get_posts(array(
    'numberposts'       => -1,
    'offset'            => 0,
    'post_status'       => 'publish',
    'author'            => '1',
    'post_type'         => 'post'
    )
);
foreach($all_active_tasks as $post) :
$category = get_the_category();
setup_postdata($post);
?>
<div class="the-post">
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <p><?php echo $category[0]->cat_name; ?></p>
</div>

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>

But what I can’t figure out is how to paginate the results to say 10 per page. I’ve looked at the official codex but nothing I tried seemed to work.

Read More

Any help is appreciated.

Related posts

3 comments

  1. You can use paged together with posts_per_page parameters. WP_Query. You can grab current page like this:

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

    and then use it in your query:

    $all_active_tasks = get_posts(array(
       'posts_per_page'    => 10,
       'post_status'       => 'publish',
       'author'            => '1',
       'post_type'         => 'post',
       'paged'             => $paged
    ));
    

    In this way if you put /page/2/ at the end of your url, the query will return the posts from 11 to 20.

    How to create the pagination itself, you can check these articles:
    here and here.

  2. Setting 'numberposts' => -1 means to get all records. You have to set for the first 10 records (0-9 records)

    'numberposts'       => 10,
    'offset'            => 0,
    

    And for next 10, (10-19 records)

    'numberposts'       => 10,
    'offset'            => 10,
    

    And for next 10, (20-29 records)

    'numberposts'       => 10,
    'offset'            => 20,
    
  3. Try this working code

    put this function in functions.php file of your active theme

    function custom_pagination($numpages = '', $pagerange = '', $paged='') {
    
    
    
      if (empty($pagerange)) {
    
        $pagerange = 2;
    
      }
    
    
    
    
    
      global $paged;
    
      if (empty($paged)) {
    
        $paged = 1;
    
      }
    
      if ($numpages == '') {
    
        global $wp_query;
    
        $numpages = $wp_query->max_num_pages;
    
        if(!$numpages) {
    
            $numpages = 1;
    
        }
    
      }
    
    
    
      $pagination_args = array(
    
        'base'            => get_pagenum_link(1) . '%_%',
    
        'format'          => 'page/%#%',
    
        'total'           => $numpages,
    
        'current'         => $paged,
    
        'show_all'        => False,
    
        'end_size'        => 1,
    
        'mid_size'        => $pagerange,
    
        'prev_next'       => True,
    
        'prev_text'       => __('<i class="fa fa-angle-double-left"></i>'),
    
        'next_text'       => __('<i class="fa fa-angle-double-right"></i>'),
    
        'type'            => 'plain',
    
        'add_args'        => false,
    
        'add_fragment'    => ''
    
      );
    
    
    
      $paginate_links = paginate_links($pagination_args);
    
    
    
      if ($paginate_links) {
    
        echo "<div class='col-md-12'><nav class='custom-pagination pagination'>";
    
          echo $paginate_links;
    
        echo "</nav></div>";
    
      }
    
    
    
    }
    

    and here is your modified code to work for pagination

    <?php
        $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    
        $all_active_tasks   = get_posts(array(
        'posts_per_page'    => -1,
        'paged'             => $paged,
        'offset'            => 0,
        'post_status'       => 'publish',
        'author'            => '1',
        'post_type'         => 'post'
        )
    );
    foreach($all_active_tasks as $post) :
    $category = get_the_category();
    setup_postdata($post);
    ?>
    <div class="the-post">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p><?php echo $category[0]->cat_name; ?></p>
    </div>
    
    <?php endforeach; ?>
    <?php if (function_exists(custom_pagination)) {
            custom_pagination(count($all_active_tasks),"",$paged);
        }?>
    <?php wp_reset_postdata(); ?>
    

Comments are closed.