0

I have searched all over the web to find some solution, but none of them are working for me, can someone help me with this and explain what is wrong? I've spent 3 hours debugging and didn't find the issue.

Here is my code:

global $paged;
global $wp_query;
wp_reset_query();

$per_page = get_option('posts_per_page');
$cat_cars = get_term_by('slug', 'cars', 'category');
$page_links_total = ceil($cat_cars->count / $per_page);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'post_type' => 'cars',
    'post_status' => 'publish',
    'cat' => $cat_cars->term_id,
    'orderby' => 'date',
    'order' => 'DESC',
    'posts_per_page' => $per_page,
    'paged' => $paged
);

$page_links = paginate_links(array(
            'base' => add_query_arg('paged', '%#%'),
            'format' => '',
            'prev_text' => __('«'),
            'next_text' => __('»'),
            'end_size' => 2,
            'mid_size' => 1,
            'type' => 'plain',
            'total' => $page_links_total,
            'current' => $paged
        ));

$car_query = new WP_Query($args);

And later i call have_posts, the_post, etc..
This part works, until I go to the second page of my pagination and get nothing found.

12
  • I can't spot any immediate problems, debugging should narrow it down, so i'm inclined to ask, what did you do to debug?
    – t31os
    Jan 5, 2011 at 10:53
  • print_r array,echo on every var,and wp_die('') - step by step. It is a problem with custom post types and pagination,but can't find the issue,and way to fix it
    – user1147
    Jan 5, 2011 at 10:56
  • @user1147: Is there a reason you don't use $car_query->max_num_pages for the total argument of paginate_links()? This will probably not solve your problem, but it saves one expensive DB query.
    – Jan Fabry
    Jan 5, 2011 at 10:57
  • 1
    @user1147: Maybe it's not expensive, but I think you don't need it, since it seems you can get the variable you need from the main post query you have to do anyway.
    – Jan Fabry
    Jan 5, 2011 at 11:02
  • 1
    This was discussed in a chat room, the problem just disappeared.
    – Jan Fabry
    Jan 5, 2011 at 13:21

2 Answers 2

1

I had a similar problem recently and determined the cause to be that when WordPress queries for posts in a category, it looks for posts with post_type equal to 'post' before it reaches the point where you query for post_type 'any' or some custom post type. This doesn't cause a problem on page 1 because even if there are no posts it calls your template. However on page 2 and so forth, it doesn't find any posts of post_type 'post' so it loads the 404 template before your template even gets a chance to modify the post_type parameter.

Is this a bug? That's a good question. I would say so because you should expect that if you register the 'category' taxonomy on other post_types, all post_types would show up in that archive, not just strictly posts.

Hopefully that helps.

0

I have this code (cleaned) on a site, calling posts from a custom post type, and at the bottom the pagination, if there's more than 10 posts:

<?php $wp_query = new WP_Query();  $wp_query->query('post_type=mycpt&showposts=10'.'&paged='.$paged); 
while ($wp_query->have_posts()) : $wp_query->the_post();?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>  
<?php endwhile; wp_reset_query(); ?>

<?php if (  $wp_query->max_num_pages > 1 ) { ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&laquo;</span> Dicas antigas', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Dicas mais recentes <span class="meta-nav">&raquo;</span>', 'twentyten' ) ); ?></div>
</div>
<?php } ?>

Hope it can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.