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:
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.
CODE-a References:
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.
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
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.
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:
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.
Thanks…
for your information i found solution :) code is here.
That’s great Furqan. For some reason I missed to answer your first comment (from April 9, 2012 at 8:40 AM).
Thanks for the code, I am sure it will help others with pagination for custom post types, similar to your case.
Boutros.
Hey,
I’m a bit late but I hope you still support this Post.
On my Site I got 3 Custom Post Types and the normal “Posts” to get a clean cut between them. Also i got 4 Pages which display 3 Posts of their CPT each, therefore I’ve used Furqans Code, which workes like a charm form my “Posts” Template but not for the “Reviews (CPT)”, “Events (CPT)” & “News (CPT)”.
I’ve pasted Furqans Snippet in all the Templates and just changed the $type variable.
If I switch to Page 2 of Reviews for example I get an 404 Error Message but I can’t find the issue.
(Check http://backbite.us/reviews )
I hope you can help me.
Ok, forget about it. I found the Answer here:
http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753
:)
very useful. Thanks Boutros AbiChedid.
i have one question about this, i created custom post and then call it different page like:
i want use pagination in this page how can i do this ?
Thanks in Advance :)
Yes this tricks is really very useful, no need to render for plugins.
Thanks for this articles.
Nice tips really liked it and also going to use it on my blog too.