Title: Custom Loop Pagination
Last modified: August 19, 2016

---

# Custom Loop Pagination

 *  Resolved [casualx](https://wordpress.org/support/users/casualx/)
 * (@casualx)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/custom-loop-pagination/)
 * Hey, I was wondering if anyone could help me with this.
 * I am using a custom bit of code to organize my posts by custom field. The code
   works fine, but it is not paginating properly. The code is as follows:
 *     ```
       <?php
   
         $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
           WHERE wposts.ID = wpostmeta.post_id
           AND wpostmeta.meta_key = 'acronym'
           AND wposts.post_type = 'post'
           ORDER BY wpostmeta.meta_value ASC
           ";
   
        $pageposts = $wpdb->get_results($querystr, OBJECT);
   
       ?>
        <?php if ($pageposts): ?>
         <?php foreach ($pageposts as $post): ?>
           <?php setup_postdata($post); ?>
   
               <!-- Post Stuff Goes Here -->
   
         <?php endforeach; ?>
   
        <?php endif; ?>
       ```
   
 * Any help would be appreciated. Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [casualx](https://wordpress.org/support/users/casualx/)
 * (@casualx)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/custom-loop-pagination/#post-953992)
 * Found the solution here:
    [http://wordpress.org/support/topic/154300?replies=11](http://wordpress.org/support/topic/154300?replies=11)
 *     ```
       $total = "your custom query goes here, but without LIMIT and OFFSET, so the total number of posts that match the query can be counted";
   
       $totalposts = $wpdb->get_results($total, OBJECT);
   
       $ppp = intval(get_query_var('posts_per_page'));
   
       $wp_query->found_posts = count($totalposts);
   
       $wp_query->max_num_pages = ceil($wp_query->found_posts / $ppp);
   
       $on_page = intval(get_query_var('paged'));	
   
       if($on_page == 0){ $on_page = 1; }		
   
       $offset = ($on_page-1) * $ppp;
   
       $wp_query->request = "your query again, but with the LIMIT and OFFSET as follows: LIMIT $ppp OFFSET $offset";
   
       $pageposts = $wpdb->get_results($wp_query->request, OBJECT);
       ```
   
 *  [albert-lau](https://wordpress.org/support/users/albert-lau/)
 * (@albert-lau)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/custom-loop-pagination/#post-954453)
 * nice code
 *  [Milan Kaneria](https://wordpress.org/support/users/milanmk/)
 * (@milanmk)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/custom-loop-pagination/#post-954457)
 * [@casualx](https://wordpress.org/support/users/casualx/)
 * Thank you very much for the solution.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Custom Loop Pagination’ is closed to new replies.

## Tags

 * [pagination](https://wordpress.org/support/topic-tag/pagination/)
 * [Query String](https://wordpress.org/support/topic-tag/query-string/)

 * 3 replies
 * 3 participants
 * Last reply from: [Milan Kaneria](https://wordpress.org/support/users/milanmk/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/custom-loop-pagination/#post-954457)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
