0

This makes no sense , I am using custom query in archive.php to do masonry layout.

$term_id        = get_queried_object_id();
$paged          = 1;
if ( get_query_var('paged') ) { 
    $paged = get_query_var('paged'); 
} elseif ( get_query_var('page') ) { 
    $paged = get_query_var('page'); 
}
$tax_query      = array();
if ( $term_id != '0' ) {
    $tax_query = array(
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $term_id
        )
    );
}

$args = array(
  'posts_per_page'  => 1,
  'post_type'       => 'post',
  'orderby'         => 'date',
  'order'           => 'desc',
  'paged'           => $paged,
  'tax_query'       => $tax_query,
  'order'           => 'DESC',
  'orderby'         => 'date'
);




$query = new WP_Query( $args );

than I do the loop and afterwards the pagination

<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php get_template_part( 'templates/blog','item'); ?>
<?php endwhile;?>
<?php custom_pagination($query->max_num_pages,1,$paged ); ?>
<?php  wp_reset_postdata(); ?>

pagination function is this one http://callmenick.com/post/custom-wordpress-loop-with-pagination

Everything showing up fine , number of pages , blog items , everything ,

enter image description here

As you can see from image above I tested multiple paginations to make sure I am doing this right. Everything is ok until you click on some pagination link. Blog pages show at most setting takes over and I get 404 page.

I have tried all suggestions on stack to reset posts_per_page , but nothing worked until I did this

function _action_set_posts_per_page( $query ) {

        $query->set( 'posts_per_page',1);


}
add_action( 'pre_get_posts', '_action_set_posts_per_page' );

this makes no sense at all , and with that action I have to run multiple checks to make sure I dont mess up someone else pagination. Why do I have to do this if my custom query already has posts_per_page defined.

Someone please enlighten me .

  • Did i get this right? You try to alter your posts shown at your archive template, by running a custom query? – iantsch Mar 20 '16 at 14:25
  • You are setting the posts_per_page for EVERY query on your page. Even in the backend for administrative purpose. why don't you put it into some conditional clause? – iantsch Mar 20 '16 at 14:26
  • @latsch yes I need to reset the posts_per_page to use my custom query. and if you read the post you will see that the action was an example before the actual setup and further use – Benn Mar 20 '16 at 16:10
1

The number of pages you have in an archive is based on the results of the main query. You can't paginate a custom query beyond the number of pages the main query contains, because it is determined to be a 404 before the template that runs your custom query is ever loaded. Altering posts_per_page and running your custom query works because you've made the number of pages match on both queries.

If the only thing you want to do differently with your custom query is alter posts_per_page, then you should remove the custom query entirely and use only pre_get_posts with some sort of is_ check to make sure you only alter the archive query you want to change.

  • thnx for explaining , the weird thing is that every peace of the query can be changed like order of items and anything else but the pagination reset. Since I need it for masonry I must use custom query in order to load items on click or set custom number of items on archive page. – Benn Mar 20 '16 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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