Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created my own template for list of entries for certain tag (tag.php), It will shows up 10 entries, and with Previous & Next link.

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type'     => 'custom_post',
'posts_per_page' => '10',
'tag'           => "$some_tag",
'paged'         => "$paged",
'post_status'   => 'publish',
'orderby'       => 'date',
'order'         => 'ASC'
); 

$loop = new WP_Query($args);
while($loop->have_posts()): $loop->the_post();
// displaying items here
endwhile;
<nav id="page-nav">
    <?php previous_posts_link('≪ Previous', $loop->max_num_pages); ?>
    <?php next_posts_link('More ≫', $loop->max_num_pages); ?>
</nav>
<?php endif; ?>
?>

It creates a More link with /tag/ta_name/page/2 but when i click on it, it shows 404.

I've checked on other blogs, tag archives pagination indeed ends with /page/N so i guess there is no problem with my url.

Where did i make the mistake or any of my settings is wrong?

share|improve this question
add comment

1 Answer

The current page number is set by the main query, not by your embedded sub-query. Also, you really should not create a second query, alter the main query instead with a filter on pre_get_posts.

add_filter( 'pre_get_posts', 'add_custom_type_to_tag_archive' );

function add_custom_type_to_tag_archive( $query )
{
    if ( ! is_main_query() or ! is_tag() )
        return $query;

    $query->set( 'post_type', array ( 'custom_type', 'post' ) );
    $query->set( 'posts_per_page', 10 );

    return $query;
}

Explanation:

When WordPress loads your tag.php it has already queried the database for tags. It knows already what tag it is, what post type and how many results there are in total and for the current page.
This is too late to change the pagination. And you should not waste that query, because it takes time.

So do not try to overwrite the main query later, alter the main query instead to get faster, more reliable results.

share|improve this answer
    
Hi toscho, i now query_posts is not recommended but i thought WP_Query would be different. So do you mean that in every case, i shouldn't be using any WP_Query or query_post, and instead use this pre_get_posts ? Yeah, your solution works! but i dont understand why... –  rei May 25 '13 at 3:49
    
@rei See my update with an explanation. –  toscho May 25 '13 at 5:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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