Thursday, August 9, 2012

Wordpress Custom Post Type Pagination

Custom post types in wordpress enables developers to do lot of things. But, when it comes to paginating it - lot of people struggle. I searched the web for the solution but could not find proper solution. So, thought of finding it on my own.

So, I created a custom post type called 'projects'.





Copy the following code in your functions.php


add_action('init', 'projects_init');
function projects_init() 
{
$project_labels = array(
'name' => _x('Projects', 'post type general name'),
'singular_name' => _x('Projects', 'post type singular name'), 
'all_items' => __('All projects'),
'add_new' => _x('Add new project', 'project'),
'add_new_item' => __('Add new project'),
'edit_item' => __('Edit project'),
'new_item' => __('New project'),
'view_item' => __('View project'),
'search_items' => __('Search in projects'),
'not_found' =>  __('No projects found'),
'not_found_in_trash' => __('No projects found in trash'), 
'parent_item_colon' => ''
);
$args = array(
'labels' => $project_labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true, 
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title', 'editor','thumbnail'),
'has_archive' => 'projects'
); 
register_post_type('projects',$args);
}

Now, create a template called 'Projects' and assign it to a page.

and copy the following code into it.

<?php 
  /*
  Template Name: Projects
  */
  get_header();
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $show_posts = 4;  //How many post you want on per page
  $permalink = 'Post name'; // Default, Post name
  $post_type = 'projects';
  
  //Know the current URI
  $req_uri =  $_SERVER['REQUEST_URI'];  
  
  //Permalink set to default
  if($permalink == 'Default') {
  $req_uri = explode('paged=', $req_uri);
  
  if($_GET['paged']) {
  $uri = $req_uri[0] . 'paged='; 
  } else {
  $uri = $req_uri[0] . '&paged=';
  }
  //Permalink is set to Post name
  } elseif ($permalink == 'Post name') {
  if (strpos($req_uri,'page/') !== false) {
  $req_uri = explode('page/',$req_uri);
  $req_uri = $req_uri[0] ;
  }
  $uri = $req_uri . 'page/';
  
  }
  
  //Query
  $wp_query->query('showposts='.$show_posts.'&post_type='. $post_type .'&paged='.$paged); 
  //count posts in the custom post type
 $count_posts = wp_count_posts('projects');

  while ($wp_query->have_posts()) : $wp_query->the_post(); 
  ?>
  <!--Do stuff-->
  <h1>
  <?php the_title(); ?>
  </h1>
  <?php the_content(); ?>
  <?php endwhile;?>
  <nav>
  <?php previous_posts_link('&laquo; ') ?>
  <?php
  $count_post = $count_posts->publish / $show_posts;
  
  if( $count_posts->publish % $show_posts == 1 ) {
  $count_post++;
  $count_post = intval($count_post);
  };
  
  for($i = 1; $i <= $count_post ; $i++) { ?>
  <a <?php if($req_uri[1] == $i) { echo 'class=active_page'; } ?> href="<?php echo $uri . $i; ?>"><?php echo $i; ?></a>
  <?php }
  ?>
  <?php next_posts_link(' &raquo;') ?>
  </nav>

  <?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
  
  get_footer();
  ?>

In the above code, you should modify the $permalink variable to the current permalink type. I tested only with 'Default' and 'Post name' permalinks.

Also, modify the $post_type variable to the custom post type name.

23 comments:

  1. Replies
    1. Thank you Lisa for your valuable comment. Good to know that it helped you.

      Delete
  2. Thanks a lot for this tutorial

    ReplyDelete
  3. Awesome, great work - much appreciated Tirumal!

    As a side note, this created some strange behaviour (404 etc.) for me when I named the page "projects" (i.e. example.com/projects).

    Worked perfectly when I changed to /our-projects.

    Thanks again!

    ReplyDelete
  4. Great, it works! Thanks for the tutorial, could you please show us how to add dots to the pagination, when there are too many pages?

    ReplyDelete
  5. The information written in the article is descriptive and well written.It is also simple to read and understand.Good Read.
    Wordpress Developers

    ReplyDelete
  6. Actually is the best I have found, looking for an script for wp pagination. Thank you!
    But... I have two questions about:
    - How I do the style (css) ? where?, I was trying for hours, in different ways and I couldn't obtain.
    - What will happen with a large number of pages? by e.g 100 pages or more?

    Thank you again!
    Regards.

    ReplyDelete
  7. This is beautiful. I'm having one problem: after setting a CPT and making the page template, I cannot see any of the posts I make appearing together on the page. I only see them individually when I click on View within the dashboard. Any idea what I might have forgotten? I have copied your code exactly, changing only the variables you indicated.

    ReplyDelete
  8. Oh, never mind... I see what I did. Sorry, and nice job again on some beautiful code!

    ReplyDelete
  9. Hi, i have a problem, I use your code and works fine, but when I try to use it for custom post type taxonomy it won't work :( can you help please

    ReplyDelete
  10. Hi there, this article has been extremely helpful. I'm using a custom post type and have everything working, apart from the part that adds a class to the active page in the pagination. Does something need to be modified in that part of the code to make this work correctly? I don't think $req_uri[1] on line 59 is right, but I can't figure it out. Can you help?

    ReplyDelete
  11. I'm not a Pro but it works for me.
    Replace the code from line 58

    for($i = 1; $i <= $count_post ; $i++) {
    if ($paged == 0) {
    $paged = 1;
    }
    ?>

    And in the link line 59 replace the IF by
    if($paged == $i) { echo 'class=active_page'; }

    ReplyDelete
  12. hello, thanks for the tutorial it works.
    btw, any way to make this work with multiple post type?
    like $post_type = 'other', 'othe2', 'other3';

    ReplyDelete
  13. Not working with WPML-counting posts in all languages altough not displaying them. Any solution?

    ReplyDelete
  14. Help ! I have a custom post type Coupon. Whenever i go to page 2 it shows me a 404 error. Please help, I cant find solutions to it.

    ReplyDelete
  15. This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff.
    wordpress tutorial

    ReplyDelete
  16. Thank You and I have a keen supply: Where Do You Get The Money To Renovate A House home renovation services near me

    ReplyDelete