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

Pagination for taxonomy.php WordPress

  • SOLVED

Similar questions have been asked before, but they are either unanswered or don't work for me.

I have a taxonomy called "businesscategories", and a term called "restaurants". The site is showing all the "restaurants", but when I go to the next page I get a 404 Error.

Here's the URL for the first page:
http://www.ethiopianwhitepages.com/?businesscategories=restaurants

Here's the URL for the next page: (gives 404 error)
http://www.ethiopianwhitepages.com/page/2/?businesscategories=restaurants

I've tried using the default permalink structure and other custom ones. Makes no difference.

Here's a simplified version of the taxonomy page:


<?php
get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>

<?php get_sidebar() ?>

<div id="content">

<h2 id="title"><?php echo $term->name ?></h2>

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts('post_type=listing&paged='.$paged); ?>

<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

<h3><?php echo the_title() ?></h3>
<?php echo the_content() ?>

</div><!-- end post -->

<?php endwhile ?>

<div id="posts_navigation">
<span id="nextlink"><?php next_posts_link('&laquo; Older Entries') ?></span>
<span id="previouslink"><?php previous_posts_link('Newer Entries &raquo;') ?></span>
</div>

<?php else : ?>
<h2 class="center">Not Found</h2>
<p>Sorry, but you are looking for something that isn't here...</p>
<?php endif ?>


</div><!-- end content -->

<?php get_footer(); ?>


Thanks!

Answers (10)

2011-05-03

Michael Fields answers:

Hi,

I have a couple questions about this.

1. What happens when you delete the call to query posts?

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;

query_posts('post_type=listing&paged='.$paged); ?>


2. Is the "businesscategories" taxonomy assigned to any post_type other than "listing"?

3. When was the last time you flushed you rewrite rules? You can do this by visiting settings -> permalinks in the admin. I should be able to visit: http://www.ethiopianwhitepages.com/businesscategories/restaurants but this is not working.

4. Could you please post the code used to register the taxonomy?

Thanks!


John Buchmann comments:

1. I don't get a 404 Error, but I also get no results on the page.

2. The taxonomy is ONLY assigned to the post type of "listing"

3. How do you "flush" the rewrite rules? I've switched from the default to all the other types of taxonomies, and even a custom one.

4.
add_action('init', 'businesscategories');
function businesscategories() {
register_taxonomy('businesscategories',
'listing',
array (
'labels' => array (
'name' => 'Business Category',
'singluar_name' => 'Business Category',
'search_items' => 'Search Business Category',
'popular_items' => 'Popular Business Category',
'all_items' => 'All Business Categories',
'parent_item' => 'Parent Business Category',
'parent_item_colon' => 'Parent Business Category:',
'edit_item' => 'Edit Business Category',
'update_item' => 'Update Business Category',
'add_new_item' => 'Add New Business Category',
'new_item_name' => 'New Business Category',
),
'hierarchical' =>true,
'show_ui' => true,
'show_tagcloud' => true,
'rewrite' => false,
'public'=>true)
);
}


When I change 'rewrite' => true to be "false", then the first page URL changes to this:
http://www.ethiopianwhitepages.com/businesscategories/restaurants

But that gives a 404 Error.


Michael Fields comments:

Re: #1&2: Since this taxonomy is only registered to a single post_type, there is no need to explicitly set the post_type via query_posts(). IMO query_posts() should only ever be used as a last resort. It is not needed at all here and will cause you more harm than good.

Re #4: Please try this instead for the rewrite argument. Using "true" really does no good and can laed to problems in my experience.

'rewrite' => array( 'slug' => 'businesscategories' )


Once the code is changed, you will want to flush the rewrite rules. This can be done by visiting "Settings" -> "Permalinks" in the WordPress Administration Panels.

Please let me how this works for you.


John Buchmann comments:

If I take away query_posts, or even just take away the "post_type" parameter, then I don't get a 404 error, but I also don't get any results on the page.

When I change the rewrite line, then when I go to the next page, it takes me to the homepage.


Michael Fields comments:

This was a step in the right direction, but from visiting your site, I see that you have deleted the changes already. This is a multi-part problem that I really can't be diagnosed correctly without access to your installation as well as your theme. If you feel comfortable providing me with this information, I would be happy to help you. Other than that, I cannot work around the noise that has been generated in this thread. There are so many incorrect answers coming from all areas that it is impossible for me to know what modifications have been made to your site.


John Buchmann comments:

I understand. I'm being pulled in all sorts of directions, so this is frustrating. Please send me an email and I'll send you FTP and WP login info. jsbuchmann [at] gmail [dot] com

2011-05-03

David Navarrete answers:

two questions:

first: did you try with pretty links?

second: how many posts per page do you have now?


David Navarrete comments:

also, are you using the PAGENAVI plugin?


David Navarrete comments:

maybe the link is wrong did you try with this link?

http://www.ethiopianwhitepages.com/?page=2&businesscategories=restaurants


David Navarrete comments:

the problem is that you are not using the pretty link options but you are using the default permalink option.


did you try change the option of permalinks?


John Buchmann comments:

That link doesn't give an error, but the results are the exactly the same as the first page. You could make page=1000 and it would still show the same results.

I've also changed the permalinks from the default to other ones and it doesn't help.

2011-05-03

derekshirk answers:

Try replacing:




<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1; query_posts('post_type=listing&paged='.$paged);
?>


With this:


<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('post_type=listings&caller_get_posts=1&paged='. $paged ); ?>


Then go to your permalinks settings page, switch back to the default, hit save, and they switch back to the custom permalinks settings you are using and switch back...does that work?


John Buchmann comments:

That doesn't do anything for me. :(

2011-05-03

Erez S answers:

Try this plugin:
http://wordpress.mfields.org/plugins/taxonomy-list-shortcode/


Erez S comments:

Or try to add this to your functions.php file:

function add_rewrite_rules($rules) {
$newrules['([^/]+)/([^\.]+).html$'] = 'index.php?$matches[1]=$matches[2]';
$newrules['([^/]+)/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?post_type=$matches[1]&locations=$matches[2]&paged=$matches[3]';

$rules = $newrules + $rules;
return $rules;
}

add_filter('rewrite_rules_array', 'add_rewrite_rules');


[[LINK href="http://wordpress.stackexchange.com/questions/9859/pagination-on-taxonomy-archive-pages-breaks"]]Source[[/LINK]]


Erez S comments:

And if non of this works, try this plugin:

http://wordpress.org/extend/plugins/category-pagination-fix/


John Buchmann comments:

Thanks, but the code and the plugin doesn't help the problem.

2011-05-03

Peter Michael answers:

Try this:


<?php
get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div id="columnswrapper" class="columns-1">
<?php get_sidebar() ?>
<div id="content" class="norightsidebar taxonomy">
<div class="inner">
<h2 id="title"><?php echo $term->name ?></h2>
<?php
$paged = get_query_var( 'page' ) ? get_query_var('page') : 1;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('post_type=listing&paged='.$paged);
?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h3><?php echo the_title() ?></h3>
<?php echo the_content() ?> </div>
<!-- end post -->
<?php endwhile ?>
<div id="posts_navigation"> <span id="nextlink">
<?php next_posts_link('&laquo; Older Entries') ?>
</span> <span id="previouslink">
<?php previous_posts_link('Newer Entries &raquo;') ?>
</span> </div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here...</p>
<?php endif; $wp_query = null; $wp_query = $temp; ?>
</div>
<!-- end inner -->
</div>
<!-- end content -->
</div>
<!-- end columnswrapper -->
<?php get_footer(); ?>


Peter Michael comments:

Also worth a note (taken from WP CODEX):

"Pagination Note: You should set get_query_var( 'page' ); if you want your query to work with pagination. Since Wordpress 3.0.2, you do get_query_var( 'page' ) instead of get_query_var( 'paged' ). The pagination parameter 'paged' for WP_Query() remains the same."

See [[LINK href="http://codex.wordpress.org/Function_Reference/WP_Query"]]http://codex.wordpress.org/Function_Reference/WP_Query[[/LINK]]


John Buchmann comments:

That didn't fix it. I also tried "page" instead of "paged" and still no difference.


Peter Michael comments:

You're using old code, see [[LINK href="http://codex.wordpress.org/Function_Reference/register_taxonomy"]]http://codex.wordpress.org/Function_Reference/register_taxonomy[[/LINK]] and [[LINK href="http://codex.wordpress.org/Function_Reference/register_post_type"]]http://codex.wordpress.org/Function_Reference/register_post_type[[/LINK]]

"When I change 'rewrite' => true to be "false", then the first page URL changes to this:
http://www.ethiopianwhitepages.com/businesscategories/restaurants

But that gives a 404 Error."

Use: 'rewrite' => array( 'slug' => 'businesscategories', 'with_front' => false ),


Peter Michael comments:

And you need to re-save your permalinks once you made the changes (Settings -> Permalinks -> Save Changes)


John Buchmann comments:

That's the first change that actually affected the site. But still doesn't work. Now when I try and go to page 2, i'm taken to the homepage instead of getting a 404 Error.


Peter Michael comments:

Try changing your init call to


add_action('init', 'businesscategories',0); // Sets priority to 0, default is 10


Might want to change the function name ('businesscategories') to something else like 'register_taxonomy_businesscategories'; not sure if that has an effect though

Also make sure you have set 'Posts per page' (Settings -> Reading) to something lower than max posts (default is 12)

2011-05-03

Sébastien | French WordpressDesigner answers:

the second page must be http://www.ethiopianwhitepages.com/?page=2&businesscategories=restaurants and no http://www.ethiopianwhitepages.com/page/2/?businesscategories=restaurants

how have you declare your custom post type ?

the problem is probably linked to $paged (as peter says), and no to your custom post type


John Buchmann comments:

I changed "paged" to "page" as per Peter's idea, but that didn't work. Here's my custom post declaration:

add_action('init', 'property_listing');

function property_listing() {
$args = array(
'description' => 'Business Listing Post Type',
'show_ui' => true,
'menu_position' => 4,
'exclude_from_search' => true,
'labels' => array(
'name'=> 'Business Listings',
'singular_name' => 'Business Listing',
'add_new' => 'Add New Listing',
'add_new_item' => 'Add New Listing',
'edit' => 'Edit Listings',
'edit_item' => 'Edit Listing',
'new-item' => 'New Listing',
'view' => 'View Listing',
'view_item' => 'View Listing',
'search_items' => 'Search Listings',
'not_found' => 'No Listings Found',
'not_found_in_trash' => 'No Listings Found in Trash',
'parent' => 'Parent Listing'
),
'public' => true,
//'taxonomies' => array('propertytype'),
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'supports' => array('title', 'editor', 'thumbnail', 'author')
);
register_post_type( 'listing' , $args );
}


Sébastien | French WordpressDesigner comments:

<blockquote>Here's a simplified version of the taxonomy page:</blockquote>
what the name of this file ?


Sébastien | French WordpressDesigner comments:

could you paste here (or send me at maildeseb @ gmail .com) all the code (with the code for taxonomy)
i want to test all that on my server.
I want to know if the problem exist on my installation


John Buchmann comments:

Thanks, I just sent you an email.

2011-05-03

Denzel Chia answers:

Hi,

Please try changing the query_posts to this.


<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
query_posts($query_string.'&post_type=listing&paged='.$paged); ?>


You probably need to add back the $query_string.

Thanks.
Denzel


John Buchmann comments:

Good idea, but that didn't help. :(

2011-05-03

Just Me answers:

Maybe a weird question but how many restaurants are present in your system? Should there be a second page?


John Buchmann comments:

There are many restaurants, plenty enough for a second page.

2011-05-03

Ryan Riatno answers:

How about to add the post_per_page to -1 ?
<?php query_posts('post_type=listing&posts_per_page=-1&paged='.$paged); ?>


Ryan Riatno comments:

Or this
<?php
$post_type = 'listing';
$tax = 'businesscategories';
$tax_terms = get_terms($tax);
$args=array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);


query_posts( $args; ) ?>


John Buchmann comments:

Making posts_per_page = -1 puts all the listings on one page, so then there isn't a "next page" link. I also tried your code snippet with a lower number like posts_per_page = 6, and still no luck.


Ryan Riatno comments:

Try changing the code for register_post_type and register_taxonomy with this :
add_action('init', 'property_listing');
function property_listing() {
{
$labels = array(
'name' => _x('Business Listings', 'post type general name'),
'singular_name' => _x('Listings', 'post type singular name'),
'add_new' => _x('Add New Listing', 'book'),
'add_new_item' => __('Add New Listing'),
'edit_item' => __('Edit Listings'),
'new_item' => __('New Listings'),
'view_item' => __('View Listings'),
'search_items' => __('Search Listings'),
'not_found' => __('No listings found'),
'not_found_in_trash' => __('No listings found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Listings'

);

$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug'=>$slug,'with_front'=>false),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'thumbnail', 'author')
);
register_post_type('listings',$args);
}
}

add_action('init', 'businesscategories');
function businesscategories()
{
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Business Categories', 'taxonomy general name' ),
'singular_name' => _x( 'businesscategories', 'taxonomy singular name' ),
'search_items' => __( 'Search Business Categories' ),
'all_items' => __( 'All Business Categories' ),
'parent_item' => __( 'Parent Business Categories' ),
'parent_item_colon' => __( 'Parent Business Categories:' ),
'edit_item' => __( 'Edit Business Categories' ),
'update_item' => __( 'Update Business Categories' ),
'add_new_item' => __( 'Add New Business Categories' ),
'new_item_name' => __( 'New Genre Business Categories' ),
'menu_name' => __( 'Business Categories' ),
);

register_taxonomy('businesscategories',array('listings'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'businesscategories' ),
));


}

I have tested it in my local development and it work


Ryan Riatno comments:

I think this is not about pagination problem as this url return 404 http://www.ethiopianwhitepages.com/listing.
It should display all listings post type


Ryan Riatno comments:

It seems that the problem was solved now. Would you mind to share where the problem is? and how you fixed that?
Thanks

2011-05-03

Khanh Cao answers:

Have you try saving your premalinks again in General - Permalinks? just click the Save Changes button. If that doesn't help then it could be .htaccess or wp rewrite error. I don't think it's coding error since the 404 page is the general one.


John Buchmann comments:

Save Changes did not fix it. Do you have a sample .htaccess text that I could try?


Khanh Cao comments:

This is the default one:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress


John Buchmann comments:

The site didn't have an .htaccess file, so I added it with the default code. That didn't help the problem.


Khanh Cao comments:

Have you disabled all plugins except the one that creates the taxonomy yet?