0
votes

I have a custom taxonomy (support) and a custom post type (question) both related.

In my taxonomy-support.php template file, I use the following query:

<?php

$current_category = get_term_by('id', get_queried_object()->term_id, 'support');

$questions = new WP_Query(array(
    'post_type' => array('question'),
    'post_status' => 'publish',
    'posts_per_page' => 2,
    'paged' => ((get_query_var('paged')) ? get_query_var('paged') : 1),
    'nopaging' => false,
    'tax_query' => array(
        array(
            'taxonomy' => 'support',
            'terms' => array($current_category->term_id)
        )
    ),
    'orderby' => 'menu_order',
    'order' => 'ASC'
));

?>

And the loop

<?php if ($questions->have_posts()): ?>

    <ul>
        <?php while ($questions->have_posts()) : $questions->the_post(); ?>
            <li>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php the_excerpt(); ?>
            </li>
        <?php endwhile; ?>
        <?php wp_reset_postdata(); ?>
    </ul>

    <div class="clearfix">
        <div class="pull-left">
            <?php previous_posts_link('&larr; ' . __('Previous', 'my-theme' ), $questions->max_num_pages); ?>
        </div>
        <div class="pull-right">
            <?php next_posts_link(__('Next', 'my-theme') . ' &rarr;', $questions->max_num_pages); ?>
        </div>
    </div>

<?php endif; ?>

As you can see I defined 2 posts per page.

When I visit the page, it shows me 2 posts then I go to page 2, it still works but when I go to page 3 or up, it shows 404.

Any idea?

I'm using WordPress 3.8.2 with no plugin installed.

Thanks

1

1 Answers

0
votes

I was happy to find your post as I was fighting for the exact same issue (less happy to find no answer yet ;) ).

Anyway I finally found something interesting here: https://wordpress.org/support/topic/set-number-of-posts-for-custom-taxonomy

This made the trick for me.

I reckon you were into the situation where the value for your global posts_per_page option (as set up in the admin) was greater than the one for your taxonomy, which made the system screw up.

Well, not that much when you think about it: during its lifecycle, having the global posts_per_page option as is, the request does not validate the "paginated" url, thus never even load your taxonomy-support.php template file, where you tell it that the posts_per_page should be different.

You then must tell it earlier, during the intialization process. The 'pre_get_posts' action is the one.

Hope it helps someone!