1

Current query:

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

$args_main = array(
    'cat'               => $my_categories_variable,
    'posts_per_page'    => 3,
    'paged'             => $paged
);

$main_posts_query = new WP_Query($args_main);
while($main_posts_query->have_posts()) : $main_posts_query->the_post();

Problem: Echoing echo $main_posts_query->max_num_pages; gives me 4, yet I am only able to navigate (via get_next_posts_link()) to www.mywebsite.com/page/3/.

Am I missing something? Please do let me know if you need more information.

Thank you!

2 Answers 2

1

You're assigning three different values to your variable:

if ( get_query_var('paged') )
{
    $paged = get_query_var('paged'); 
}
else if ( get_query_var('page') )
{
    $paged = get_query_var('page');
}
else
{
    $paged = 1;
}

Short explanation:

  1. On single.php, singular.php and other single view templates: (int) $page Is the page of the post, as specified by the query var page: get_query_var( 'page' );
  2. On all sorts of archive.php and similar archive/post type list view templates: (int) $paged Is the global variable contains the page number of a listing of posts (as in archives).

Core uses a lot of (slightly confusing) globals (so don't blame yourself).

1
  • Hey, thanks. So what exactly am I doing wrong? I still need proper pagination in my blog, and assumed that $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; would work properly.
    – cr0z3r
    Commented Aug 17, 2013 at 11:43
0

I have used the following code (which is basically the same as yours) but it did not work for me when using WP_Query($args).

if ( get_query_var( 'page' ) > 1) { 
    $paged = get_query_var( 'page' ); 
} elseif ( get_query_var( 'paged' ) > 1) { 
    $paged = get_query_var( 'paged' ); 
} else { 
    $paged = 1; 
}

Although it's not recommended, pagination began working for me when I changed WP_Query($args) to query_posts($args) and then adjusted The Loop accordingly.

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }

$args_main = array(
    'cat'               => $my_categories_variable,
    'posts_per_page'    => 3,
    'paged'             => $paged
);

query_posts($args_main);
while(have_posts()) : the_post();

NOTE: As Kaiser noted in his comment below query_posts() can screw things up. At the very least reset the query after The Loop.

wp_reset_query();

2
  • Please, don't recommend using query_posts(). It's an internal funciton that shouldn't be used as it overrides the global $wp_query and disconnects it from $wp_the_query. Which means you just destabilized your whole system and ever second query will break somewhere hidden. Have fun debugging that.
    – kaiser
    Commented Aug 15, 2013 at 19:28
  • Thank you. I am avoiding query_posts at all costs, which is why I'm swapping my code to WP_Query (hence my problem now).
    – cr0z3r
    Commented Aug 17, 2013 at 11:46

Your Answer

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

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