Pagination for Custom Post Types in WordPress Without a Plugin

For a WordPress blog, pagination refers to displaying a limited number of posts per page. Pagination allows users the ability to navigate much easier and deeper into the archives. In this post, I will show that my previous tutorial about adding Pagination without a plugin in WordPress, can also be used on Custom Post Types without any modifications.

I will also show the steps I took to test my previous tutorial´s pagination code and show that it is still valid for custom post types.

What are Custom Post Types?

WordPress can hold and display many different types of content, which are: Post, Page, Attachment, Revision, Nav Menus and also Custom Types that you define. Think of Custom Post Types as Custom Content Types.

Since version 3.0, WordPress added the capability to create and manage content via custom post types. Custom post types represent any type of content you want and add the flexibility to have more than just posts and pages for content by allowing users to register their own post types.

Ideas For Custom Post Types

While the default WordPress post types are adequate for most Websites, other Websites need alternatives like in the case of: Photo database, Real Estate listings, Videos/Audios database, Movies, Books, Events calendars, and others.

WordPress Pagination for Custom Post Types

The question here that I am trying to answer: Can CODE-X of my previous WordPress Pagination tutorial, be used for Custom Post Types? and the answer is YES as shown below.

Here are the 3 steps I followed to test and check that CODE-X (along with CODE-Y and CODE-Z) of my previous pagination tutorial does in fact work for Custom Post Types for this WordPress blog.

To be clear I already implemented for this blog the pagination code as detailed in my pagination tutorial.

Step 1. I Opened functions.php file located in my theme´s folder and added the following code CODE-a. Saved the functions.php file and Uploaded it to the server.

CODE-a :: Creating a Specific Custom Post Type

<?php 
/*****************************************************************
* @Date: January 10, 2011
* @Description: Function that creates the custom post type 
* (Products), and adds it to the the dashboard.
* @Tested on: WordPress version 3.2.1 (Works on WP version >= 3.0)
******************************************************************/ 

add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'multiple_product',
		array(
			'labels' => array(
			'name' => __( 'Products' ),
			'singular_name' => __( 'Product' )
			),
		'public' => true,
		'has_archive' => true,
		)
	);
}
?>

CODE-a Notes:

Adding a custom type to WordPress is done via the register_post_type() function, which allows you to define the post type and how it operates within WordPress. CODE-a is a very basic example of adding a custom post type.

After I added CODE-a to the functions.php file, I ended up with a new panel in the admin menu of the WordPress dashboard as shown in the image below:

Created a new Panel in WordPress dashboard called 'Products'.

For testing purposes I chose a very simple custom post type named “Product”. It has two major arguments with it. The first one is the “labels”, which define the name of the type in both plural and singular forms. The second one is “public”, which is a predefined flag to show the post type in the WordPress dashboard and to show it up on the main Website itself, if it is queried for.

Step 2. I Logged-in to the WordPress dashboard. I located the new “Products” panel and created a total of 13 custom posts for the “Products” type, as shown in the image below.

Created 13 custom posts for the 'Products' type.

Step 3. Now I need to display these Custom posts of type “Products” on the Blog. For testing and simplicity reasons, I want to show them on the Main page. I again opened the functions.php file located in my theme´s folder and added the following code CODE-b. I saved the functions.php file and uploaded it to the server.

CODE-b :: Displaying the Posts of type ´Products´ on the Blog´s Main Page

<?php 
/***********************************************************************
* @Date: January 10, 2011
* @Description: Function that displays in the Main page of the Blog the 
* Posts of Custom Type 'Products' that were created in the dashboard. 
* This is done for Testing purposes only.
* @Tested on: WordPress version 3.2.1 (Works on WP version >= 3.0)
************************************************************************/ 

//Hooks a function to a specific filter action.
add_filter( 'pre_get_posts', 'bac_get_posts' );
function bac_get_posts( $query ) {
	if ( is_home() && false == $query->query_vars['suppress_filters'] )
		//Only display the Posts of Custom Type 'Products' in the Main page.
		$query->set( 'post_type', array( 'multiple_product' ) );
		
        //Display the Posts of Custom Type 'Products' and other posts in the Main page.
		//$query->set( 'post_type', array( 'post', 'multiple_product' ) );
	return $query;
}
?>

CODE-b Notes:

This code is good enough for testing purposes. I only chose to display the Posts of type ´Products´ in the main page. Usually this is not how custom Post types are displayed in a WordPress Website.

If you want to display your regular posts and custom posts on the Blog´s Main page, use Line 18 (which is now commented out). Don´t forget to comment out line 15 first.

CODE-b References:

Creating Custom Post Types Template Files | The Right Way

WordPress added support for single-type-template in Version 3.0 and for archive-type-template in Version 3.1.

Single template

In the same way that posts are shown on their own page with single.php, custom post types will use single-{posttype}.php if it is available. For the above example, you could create a single-multiple_product.php file and the Products posts would be shown using that template.

Archive template

In the same way that posts are shown on their own archive with archive.php, custom post types will use archive-{posttype}.php if it is available. For the above example, you could create a archive-multiple_product.php file and the Products posts would be shown using that template.

References

  1. A Page of Posts for a Custom Post Type
  2. Pagination for custom post type on page template
  3. Post_Types » Template_Files

Result | Does My Pagination Code Also Work on Custom Post Types?

The pictures below shows the pagination generated for the Posts of Custom Type “Products” that I temporarily created for this blog. It shows that (CODE-X, CODE-Y and CODE-Z) in my previous pagination tutorial still works for custom post types without any modifications.

Pagination for the Custom Post Type named 'Products' - Page1.

Pagination for the Custom Post Type named 'Products' - Page2.

Pagination for the Custom Post Type named 'Products' - Page4.

You can adjust how many posts are displayed on the main page, by going to the WordPress dashboard (Settings -> Reading). I created 5 pages from the 13 Posts of Custom Type “Products” by reducing the number of posts displayed per page to 3 as shown in the image below:

Adjusting how many posts are displayed on the main page.

Your Turn to Talk

This post shows that my previous tutorial about adding custom WordPress pagination without a plugin can also be used for custom post types without any modifications.

If you are using Custom Post Types on your blog, and you like to paginate your custom posts without a plugin, go ahead and follow my previous wordpress pagination tutorial.

If you run into problems or have anything to add or something else to say, please share your opinion in the comments section below. Your opinion matters, unless it is a Spam.

References

  1. Function Reference « WordPress Codex
  2. Post Types
  3. Custom post types in WordPress
  4. Taxonomies

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

8 Responses to “Pagination for Custom Post Types in WordPress Without a Plugin”

  1. Edy Zulyza says:

    Thanks…

  2. Furqan says:

    for your information i found solution :) code is here.

    <?php
      $type = 'book';
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args=array(
        'post_type' => $type,
        'post_status' => 'publish',
        'paged' => $paged,
        'posts_per_page' => 2,
        'caller_get_posts'=> 1
      );
      $temp = $wp_query;  // assign original query to temp variable for later use
      $wp_query = null;
      $wp_query = new WP_Query();
      $wp_query->query($args);
    
      while ($wp_query->have_posts()) : $wp_query->the_post();
    ?>
            <li><a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
              </a>  </li>
              
    <?php endwhile;?>
    <?php if ( $wp_query->max_num_pages > 1 ) : ?>
    <div class="navigation">
       <!-- REMOVE default page navigation once your custom pagination is working. -->
       <?php //REMOVE DEFAULT:  next_posts_link('«« Older Posts')
            //REMOVE DEFAULT:   previous_posts_link('Newer Posts »»') ?>
     
       <!-- ADD Custom Numbered Pagination code. -->
       <?php if(function_exists('pagenavi')) { pagenavi(); } ?>
       </div>
    <?php endif; ?>  
            
    <?php wp_reset_query(); ?>
    
  3. Furqan says:

    very useful. Thanks Boutros AbiChedid.
    i have one question about this, i created custom post and then call it different page like:

    <?php query_posts('post_type=book'); ?>
    <?php while (have_posts()) : the_post(); ?>
              <?php the_title(); ?>
    <?php endwhile;?>		
    <?php wp_reset_query();?>
    

    i want use pagination in this page how can i do this ?
    Thanks in Advance :)

  4. Shivkumar @teechworld says:

    Yes this tricks is really very useful, no need to render for plugins.
    Thanks for this articles.

  5. Sheshnath says:

    Nice tips really liked it and also going to use it on my blog too.