Custom Post Types & Pagination

I came across a small issue earlier in the week where we had a website using custom post types but the pagination was not working correctly. Every time you went past the 2nd page, you would get a 404 page not found error. It seems that WordPress 3.0 and custom post types are not working so great however there is a small fix to get around this issue.

The trick is to wrap the code with an add_filter and function. Like so:

if (taxonomy_exists($wp_query->query_vars['taxonomy'])) {
	add_filter('pre_get_posts', 'postcount_filter');
	function postcount_filter($wp_query) {
		$type = 'post_type';
		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
		$args=array(
		'post_type' => $type,
 		'post_status' => 'publish',
		'paged' => $paged
		);
		$wp_query->query($args);
	}
}
This entry was posted in Development and tagged , , . Bookmark the permalink.

2 Responses to Custom Post Types & Pagination

  1. Castle says:

    Hi David,

    I am facing the same issue. I am php newbie, so could you please give me more details about your above code. How do I use it?

    Best,
    Daniel

  2. David says:

    Hi Daniel,

    Create your taxonomy template file (ie: taxonomy-artists.php) and include the above code at the beginning of the page, right under the header include.

    Let me know how you go

    Cheers
    David

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>