How to include pagination in a Wordpress Custom Post Type Query

Solution for : How to include pagination in a Wordpress Custom Post Type Query

If we have to get the list for custom post type "project". Below is simple code to achieve our goal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$the_query = new WP_Query(
 array('posts_per_page'=>20,
 'post_type'=>'project',
 'paged' => get_query_var('paged') ? get_query_var('paged') : 1)
 );
  
  
while ($the_query -> have_posts()) : $the_query -> the_post();
 
echo '<div>';
 echo '<a href="'.get_the_permalink().'">';
 echo get_the_title();
 echo '</a>';
echo '</div>';
endwhile;
  
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );
 
wp_reset_postdata();

No comments:

Post a Comment