Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Lucas Santos
Lucas Santos
19,315 Points

Wordpress pagination gives 404 unless I set “Blog pages show at most” to 1 in reading

Ok so let me start of by telling you guys exactly what im doing and in what page. I am developing locally on MAMP and I am using "Custom Post Types" and "Custom Fields" Plugin. So I have a custom post type of "Products" and a custom taxonomy of "Types"

Anyways im trying to use my pagination in "Taxonomy.php" because thats where im going to display my custom post types of "products" with my custom taxonomy "types' and use conditionals to generate different content on that same taxonomy.php.

So this is my code in taxonomy.php that I got from css-tricks

<?php
  $temp = $wp_query; 
  $wp_query = null; 
  $wp_query = new WP_Query(); 
  $wp_query->query('showposts=3&post_type=products'.'&paged='.$paged); 

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

// my content from my custom field that I want paginated

<p><?php the_field('image'); ?></p>

<?php endwhile; ?>

<nav>
    <?php previous_posts_link('&laquo; Newer') ?>
    <?php next_posts_link('Older &raquo;') ?>
</nav>

<?php 
  $wp_query = null; 
  $wp_query = $temp;  // Reset
?>

With this method of displaying the paginations I got from css-tricks it works great but the only problem is I keep getting a 404 error message whenever I try to go to the next page UNLESS I SET MY "Blog pages show at most" TO 1 IN SETTINGS > READING. If I do that then it works otherwise it gives me the 404 page. My permalinks are set to Post Name by the way.

So I can see my pagination here:

http://localhost/MyWebsite/types/cars/

but when I get here I get the 404 page:

http://localhost/MyWebsite/types/cars/page/2/

I have read about the Flush Method and that does not work and I only have 2 plugins activated custom post types and fields and those are not the problem. Any suggestions would be greatly appreciated.

4 Answers

Try to add

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

Before your custom query line.

It worked once, Now i'm using page templates instead of taxonomy.php, it's always has that pagination issue. And sometimes i use this function to set posts per page

<?php

function my_post_queries( $query ) {
    // not an admin page and it is the main query
    if (!is_admin() && $query->is_main_query()){

        if(is_tax()){
            // show 30 posts on custom taxonomy pages
            $query->set('posts_per_page', 30);
        }
    }
}
add_action( 'pre_get_posts', 'my_post_queries' );
Lucas Santos
Lucas Santos
19,315 Points

Hey thanks for the reply Muhammad! So to give you guys an update no defining $paged did no solve the problem but I have great news. So after 17 hours of debugging, researching and diagnosing the problem iv created solution. Now I know this is actually a common and well known problem when it comes to custom taxonomies and custom post types so I want to share with you guys exactly what I did, what the problem was and how I solved it. Im actually about to go to bed rite now but tomorrow im going to type up an actual full diagnosis of what happened and what I did and i'll share it with treehouse community so hopefully it can help other when facing this issue.

I've got a similar problem and would love to hear your solution, thanks!

Lucas Santos
Lucas Santos
19,315 Points

Just posted answer.

Lucas Santos
Lucas Santos
19,315 Points

Ok so iv actually come up with a couple of solutions and just recently iv come up with the correct one. So I had actually achieved what I wanted by changing the main query but it wasn't a good idea to change the main query so iv just found a solution that allows me to get the paginations to work on taxonomy.php by altering the main query with pre_get_posts

Here is my code:

<?php 
add_action( 'pre_get_posts', function($q) { 
if( !is_admin() && $q->is_main_query() && $q->is_tax() ) { 
$q->set ('post_type', array( 'post', 'products' ) ); 
} 
});

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

//loop content
<?php the_field( 'image' ); ?>

<?php endwhile; ?> 
<?php endif; ?> 
<?php 
global $wp_query; 

$total_pages = $wp_query->max_num_pages; 

if ($total_pages > 1){ 

$current_page = max(1, get_query_var('paged')); 

echo paginate_links(array( 
'base' => get_pagenum_link(1) . '%_%', 
'format' => '/page/%#%', 
'current' => $current_page, 
'total' => $total_pages, 
'before_page_number' => '<div class="pagination-navigation">', 
'after_page_number' => '</div>' 

)); 
} 
?>

This is the important part and can actually go in functions.php

<?php 
add_action( 'pre_get_posts', function($q) { 
if( !is_admin() && $q->is_main_query() && !$q->is_tax() ) { 
$q->set ('post_type', array( 'post', 'products' ) ); 
} 
});
?>

Just replace "Products" with your custom post type.

You can set set new arguments like so:

$q->set ('post_type', array( 'post', 'products' ) ); $q->set ('posts_per_page', 5 );

I just set however many posts I want to display in the admin area of Settings > Reading > Blog pages show at most

And thats how I fixed my pagination problem with custom post types and taxonomies in taxonomy.php.

Thanks Lucas, will try this out!

I have another question that I will start a new question for, and hope you can help me out. It is regarding setting up my custom taxonomy correctly. Thanks again.

Lucas Santos
Lucas Santos
19,315 Points

no problem feel free to ask, I'll take a look at it.