Title: Posts pagination with custom SQL
Last modified: August 22, 2016

---

# Posts pagination with custom SQL

 *  [benjamincrozat](https://wordpress.org/support/users/benjamincrozat/)
 * (@benjamincrozat)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/posts-pagination-with-custom-sql/)
 * Hello,
 * I have a really weird problem. I have a loop in archive.php that displays posts
   in a custom way. When I access to it [http://domain.ext/event/](http://domain.ext/event/)(
   this is a custom post type), it works fine. Here the generated query:
 *     ```
       SELECT SQL_CALC_FOUND_ROWS $wpdb->postmeta.meta_value AS event_date, $wpdb->posts.*
       FROM $wpdb->postmeta
       RIGHT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
       $wpdb->postmeta.meta_key = 'event_date'
       WHERE $wpdb->postmeta.meta_value >= NOW()
       ORDER BY $wpdb->postmeta.meta_value ASC
       LIMIT $start, $posts_per_page
       ```
   
 * But when I want to see a specific category, pagination doesn’t work anymore (
   [http://domain.ext/category/a-category/](http://domain.ext/category/a-category/)):
 *     ```
       SELECT SQL_CALC_FOUND_ROWS $wpdb->postmeta.meta_value AS event_date, $wpdb->posts.*
       FROM $wpdb->postmeta
       RIGHT JOIN $wpdb->posts ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
       $wpdb->postmeta.meta_key = 'event_date'
       RIGHT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       RIGHT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       WHERE $wpdb->term_taxonomy.taxonomy = 'category'
       AND $wpdb->term_taxonomy.term_id IN ($category_id) AND
       AND $wpdb->postmeta.meta_value >= NOW()
       ORDER BY $wpdb->postmeta.meta_value ASC
       LIMIT $start, $posts_per_page
       ```
   
 * Here is my (simplified) template:
 *     ```
       <?php
       $category_id = get_query_var( 'cat' ); // Used in the query.
   
       $posts_per_page = get_option( 'posts_per_page' );
   
       $start = ( get_query_var( 'paged' ) - 1 ) * $posts_per_page;
       $start = $start >= 0 ? $start : 0; // We need a positive number.
   
       $sql = "SELECT SQL_CALC_FOUND_ROWS $wpdb->postmeta.meta_value ...";
   
       if ( is_category() ) {
         $sql .= "RIGHT JOIN ...";
       } else {
         $sql .= 'WHERE ';
       }
   
       $sql .= "$wpdb->postmeta.meta_key = 'event_date' ... LIMIT $start, $posts_per_page";
   
       $posts = $wpdb->get_results( $sql, OBJECT );
       ?>
   
       <?php if ( $posts ) : global $post; ?>
   
         <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
   
           <?php get_template_part( 'loop' ); ?>
   
         <?php endforeach; ?>
   
         <?php get_template_part( 'posts-pagination' ); ?>
   
       <?php endif; ?>
       ```
   
 * I also tried to manually set max_num_posts and found_posts in $wp_query, pagination
   appears, but I get a 404 error.

The topic ‘Posts pagination with custom SQL’ is closed to new replies.

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [pagination](https://wordpress.org/support/topic-tag/pagination/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * 0 replies
 * 1 participant
 * Last reply from: [benjamincrozat](https://wordpress.org/support/users/benjamincrozat/)
 * Last activity: [11 years, 4 months ago](https://wordpress.org/support/topic/posts-pagination-with-custom-sql/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
