Wordpress custom page type taxonomy pagination


#1

I have a custom post type called “portfolio” with a custom taxonomy called “portfolio-category”. Here’s the taxonomy-portfolio-category.php code:

$term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;

The problem is this page is bound by Settings > Reading > Blog pages show at most 5. Five works good for the style of my blog index, but for the style of my portfolio pages I need more like 24 per page. How do I override what’s in my Settings for these custom taxonomy page templates? Hope someone can help. Thanks! :slight_smile:


#2

Try using query posts

query_posts(array(‘post_type’ => ‘portfolio’, ‘posts_per_page’ => 24, ‘taxonomy’ => ‘portfolio_category’ ));


#3

Thanks for the suggestion. That’s the solution I’ve seen floating around the net. Using query_posts I coded the below. This creates 3 pages as it should. Page 1 displays, but pages 2 (/page/2/) and 3 give me a “page not found”. I never seem to get further than this spot. :frowning:

global $wp_query;
$term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );
$args = array_merge( $wp_query->query, array(
‘post_type’ => ‘portfolio’,
‘posts_per_page’ => 1,
‘tax_query’ => array( array(
‘taxonomy’ => ‘portfolio-category’,
‘field’ => ‘slug’,
‘terms’ => $term,
) ),
) );
query_posts( $args );


#4

Try this:


  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $args=array(
    'post_type' => portfolio,
    'post_status' => 'publish',
    'taxonomy’ => 'portfolio_category',
    'paged' => $paged,
    'posts_per_page' => 24,
    '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);

if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();

...

endwhile; endif;



#5

Here’s a copy of my code just to make sure we’re on the same page, but it didn’t work. So it seems like the only way to override those Reading settings for posts it create a custom query on the page.

$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args = array(
‘post_type’ => portfolio,
‘post_status’ => ‘publish’,
‘taxonomy’ => ‘portfolio-category’,
‘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);

if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();

I’ve been working on this problem all week. I swear I’ve read every article online about this, haha! This has been by far my biggest WP challenge to date.


#6

Just an update, nothing has been solved here. I’m starting to wonder if I’ve found a Wordpress bug.


#7

always use wp_reset_query(); after your looping code :slight_smile:


if(have_posts()):
while (have_posts()): the_post();




endwhile; endif;

wp_reset_query();

Good luck


#8

Nope, didn’t work. Thanks for the suggestion though.


#9

Hmm try this too:

delete the if statement


if($wp_query->have_posts()) : 

leave just this:

 while($wp_query->have_posts()) : $wp_query->the_post();

remove the endif from the end of the loop and paste this:


$wp_query = null; $wp_query = $temp;



#10

No, that didn’t work either. :frowning: Ok, this will be a bit long-winded, but it will provide the best possible knowledge to troubleshoot my problem. All the information in my original post is still true. I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.

I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:

page-home.php (used as a Page template on a static front page called “Home”)

$paged = 1;
if ( get_query_var(‘paged’) ) $paged = get_query_var(‘paged’);
if ( get_query_var(‘page’) ) $paged = get_query_var(‘page’);
$i = 0;
$loop = new WP_Query( array( ‘post_type’ => ‘portfolio’, ‘paged’ => $paged, ‘posts_per_page’ => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( ‘wp_pagenavi’ ) ) {
wp_pagenavi( array( ‘query’ => $loop ) );
wp_reset_postdata();
}

Pagination works!

page-portfolio.php (used as a Page template on a Page called “Work”)

$i = 0;
$loop = new WP_Query( array( ‘post_type’ => ‘portfolio’, ‘paged’ => get_query_var( ‘paged’ ), ‘posts_per_page’ => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( ‘wp_pagenavi’ ) ) {
wp_pagenavi( array( ‘query’ => $loop ) );
wp_reset_postdata();
}

Pagination works!

taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)

$term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( ‘posts_per_page’ => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( ‘wp_pagenavi’ ) ) {
wp_pagenavi();
}

The page 1 (http://site.com/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 the site defaults to index.php and shows “Page not found”. Pagination fails!

Womp womp. Hopefully I can resolve this soon. It’s holding up my theme development. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions for anyone. Please help!


#11

I’ve made some headway! There’s still an issue, but here’s the code that currently works …

function my_modify_posts_per_page() {
add_filter( ‘option_posts_per_page’, ‘my_option_posts_per_page’ );
}
function my_option_posts_per_page( $value ) {
return 2;
}
add_action( ‘init’, ‘my_modify_posts_per_page’, 0);

The problem is this makes ALL loops return only 2 posts per page. Right now I’m working on getting a conditional statement in there that only checks for taxonomy “portfolio-category”. I’ll let you know when I’ve finally figured this out.


#12

Ok, I’ve done it. There must be a more graceful way to do this, but I’ve put this code in my functions.php file and it works …

$option_posts_per_page = get_option( ‘posts_per_page’ );
add_action( ‘init’, ‘my_modify_posts_per_page’, 0);
function my_modify_posts_per_page() {
add_filter( ‘option_posts_per_page’, ‘my_option_posts_per_page’ );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( ‘portfolio-category’) ) {
return 2;
} else {
return $option_posts_per_page;
}
}

If anyone knows a better way to write this code please hook me up! Thanks. :slight_smile:


#13

I couldn’t really find another way to do this. This solution worked perfectly for me. Thanks!


#14

I know this is an old thread, but I was looking for a solution to “posts_per_page” in taxonomy templates and never did find an answer. I finally figured out something that worked for me.


$term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );

query_posts( array( ‘post_type’=>‘portfolio’, ‘portfolio-category’ => $term->name, ‘posts_per_page’=>20, ‘paged’=>$paged ) );

if (have_posts()) : while (have_posts()) : the_post();

Where ‘post_type’ is the custom post type and ‘portfolio-category’ is the custom taxonomy. I’ve tried to do this WP_query and it doesn’t work. I hope this is useful for someone else


#15

May be you need to enable search to enable pagination

While declaring custom taxonomy you should disable search excluding.

exclude_from_search => false

This fixed my problem. I have very hard time to find this solution. Hope this helps everyone.

My code is:

register_post_type( 'lifestyle',
                array( 
				'label' => __('Lifestyle', 'tmi'), 
				'public' => true, 
				'show_ui' => true,
				'show_in_nav_menus' => true,
				'rewrite' => true,
				'hierarchical' => true,
				'menu_position' => 5,
				'exclude_from_search' =>false,
				'supports' => array(
				                     'title',
									 'editor',
                                     'thumbnail',
                                     'excerpt',
                                     'revisions')
					) 
				);
				
	register_taxonomy('lifestylecat', __('lifestyle', 'tmi'),array('hierarchical' => true, 'label' =>  __('Categories', 'tmi'), 'singular_name' => __('Category', 'tmi'))
	);

#16

Hey jcarroll, I think you should check this tutorial about pagination WordPress. Hope it will help to resolve your issue.