Wordpress custom page type taxonomy pagination

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:

Try using query posts

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

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 );

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;


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.

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

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

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

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;


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!

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.

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:

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

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

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'))
	);

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

Hi,
Still, now this isn’t resolved. But which some changes in the query and the pagination it worked. I created custom pagination where it will pass page variable through variable page instead of paged and then update the query like this:

global $wp_query;
if(array_key_exists('page', $_REQUEST)){
    $args = array_merge( $wp_query->query, ['paged'=> $_REQUEST['page']]);
    query_posts( $args );
  }

Custom pagination function:

function custom_pagination() {
	global $wp_rewrite, $wp_query;
	if(isset($wp_query->query_vars['paged'])&&$wp_query->query_vars['paged']>1) {
		$current = $wp_query->query_vars['paged'];
	} elseif(isset($wp_query->query_vars['page'])&&$wp_query->query_vars['page']>1) {
		$current = $wp_query->query_vars['page'];
	} else {
		$current = 1;
	}
	$pagination = array(
		'base' => @esc_url(add_query_arg('page','%#%')),
		'format' => '',
		'total' => $wp_query->max_num_pages,
		'current' => $current,
		'show_all' => false,
		'prev_text'    => 'Previous',
		'next_text'    => 'Next',
		'type' => 'list',
	);
	if( $wp_rewrite->using_permalinks() )
		$pagination['base'] = user_trailingslashit( trailingslashit( esc_url(remove_query_arg(['s', 'page'],get_pagenum_link(1)) ) ) . '?page=%#%', 'paged');
	if( !empty($wp_query->query_vars['s']) )
		$pagination['add_args'] = array('s'=>get_query_var('s')); ?>
	<div class="w-clearfix"><?php echo paginate_links($pagination); ?></div>
	<div class="none">
		<?php wp_link_pages(""); ?>
	</div>
<?php
}