1

WP_Query custom query showing same only 10 posts on all pages when set to 5. Here is my code:

Updated code:

<?php 
global $wp_query;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
// WP_Query arguments
$args = array ('pagination'             => true,
'posts_per_page'         => 5,
'ignore_stickie_posts' => true,
'page'               => $paged);

// The Query
$wp_query_new  = new WP_Query( $args );
if ( $wp_query_new ->have_posts() ) {
while ( $wp_query_new ->have_posts() ) {
    $wp_query_new ->the_post();
?>  ...<?php 
}
?>
<?php
$big = 999999999; // need an unlikely integer

$pages = paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?page=%#%',
    'current' => max( 1, get_query_var('page') ),
    'total' => $wp_query_new ->max_num_pages,
    'type'  => 'array',
) );
if( is_array( $pages ) ) {
    $paged = ( get_query_var('page') == 0 ) ? 1 : get_query_var('page');
    echo '<ul class="pagination">';
    foreach ( $pages as $page ) {
            echo "<li>$page</li>";
    }
   echo '</ul>';
    }
   ?>
    <?php } else {

?>
<p><?php _e('Not found, what you are looking for.'); ?></p>
<?php } 

// Restore original Post Data
wp_reset_postdata();
?>

Can anyone help me in this regard?

1

One of two things is happening that I can think of:

  1. You have sticky posts showing up. Try adding an ignore_stickie_posts argument.

    $args = array (
      'pagination'             => true,
      'posts_per_page'         => 5,
      'ignore_stickie_posts' => true,
    );
    

    See: https://wordpress.stackexchange.com/a/85658/21376

  2. You have a poorly written (too global) pre_get_posts filter. Something like:

    function naughty_pre_get_posts( $query ) {
      $query->set( 'posts_per_page', 1 );
    }
    add_action( 'pre_get_posts', 'naughty_pre_get_posts' );
    

Also, when you name your query $wp_query you clobber the main query on the page and that can cause unexpected/unpredictable effects. Choose a new name for the custom query, or if you are attempting to alter the main query proper, convert the whole thing into a pre_get_posts filter.

  • Thanks for your quick reply. I will give it a try and let you know :) – user3388361 Jul 17 '15 at 7:48
  • Hi,Tried to add 'ignore_stickie_posts' => true also changed 'paged' to 'page'. I have updated code in my question. But it dosent seem working. – user3388361 Jul 17 '15 at 15:37
  • also it is showing same 10 posts on all pages even if it is set to show 5 posts. – user3388361 Jul 17 '15 at 15:45
  • So, the other option that there is a bad filter? Did you disable your plugins? Maybe even switch themes? – s_ha_dum Jul 17 '15 at 16:30

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.