Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pagination & Filter Not Working WordPress


I'm using a custom taxonomies called "type" for filtering, which works fine, but the pagination is no longer working. If I'm not using the foreach ( ), the pagination is working fine, but the taxonomy filter no longer works.

You can see the code below in action at this development site:

I've set the posts_per_page to 3.

I've tried every combination I know, but haven't found a solution.

<div id="container" class="variable-sizes clearfix infinite-scrolling">

$tax = 'type';
$tax_terms = get_terms($tax);

if ($tax_terms) {
foreach ($tax_terms as $tax_term) {

if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
$paged = 1;

$args = array(

$tax => $tax_term->slug,
'post_type' => 'portfolio',
'posts_per_page' => 3,
'paged' => $paged );


<?php $my_query = new WP_Query($args); ?>

<?php if ( $my_query->have_posts () ) { ?>

<?php while ( $my_query->have_posts ()) : $my_query->the_post(); $count++; global $post; ?>

<?php include (TEMPLATEPATH . '/_framework/includes/portContent.php'); ?>

<?php endwhile; ?>

<?php } } } ?>

</div><!-- end Container -->

<?php if (function_exists("pagination")) {
} ?>

Answers (2)


Michael Fields answers:

Hi Justin,

The first thing is that you should not use the word "type" for a custom taxonomy. It is a reserved word and will cause many, many problems that seem unrelated to taxonomies. I've been down this road many times :) I would suggest changing the name to something that is not in the [[LINK href=""]]reserved words list[[/LINK]].

As for the paging, I would suggest using a custom taxonomy template instead of a page. You can read more about how these taxonomy archives work in [[LINK href="null"]]this section[[/LINK]] of the template hierarchy codex page. Once you have this archive template set up, you can use next_posts_link() and previous_posts_link() without dealing with query vars at all.

Justin Young comments:

I'll check that out, do something studying on taxonomies. I just can't seem to solve this problem, even though it seems like it should be working. I think it has something do with the foreach() loop.

Michael Fields comments:


The problem here stems from mixing a custom query with the "natural" query that WordPress produces for the "page". In my experience, "going against the grain" with WordPress is prone to errors especially down the road. Using a taxonomy template is by far the best solution here as you will be able to use WordPress template tags without modification.

On another note, many issues can be fixed by using WordPress get_*() functions instead of creating your own WP_Query objects. In all of the years that I have used WordPress, I've only ever *needed* to directly create a class of this object once. I'll always use get_post(), get_page() or get_children() instead if possible. And for most cases it is possible :)

Changing the name of the taxonomy is <strong>very</strong> important as well. "Type" is a reserved word and is actually used in 3.1 as a rewrite slug for Post Format archives. As far back as version 2.9 this would cause headaches for me ... the biggest of which had to do with not being able to search within media upload modal! I know, it sounds weird and totally unrelated, but using "Type" as a taxonomy name is a really, really bad idea.



Daniele Raimondi answers:

Try to call wp_reset_postdata() after the endwhile (i.e. for each custom loop you make) as suggested [[LINK href=""]]here[[/LINK]], inthe first example box. It resets the global vars setted up during your new custom loop.