I've got the following loop in index.php file:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$post_per_page = 30;
$do_not_show_stickies = 1;

    'post_type' => array ('portfolio'),
    'order' => 'DESC',
    'paged' => $paged,
    'posts_per_page' => $post_per_page,
    'ignore_sticky_posts' => $do_not_show_stickies
$wp_query = new WP_Query($args);
if( have_posts() ) : 
        while ($wp_query->have_posts()) : $wp_query->the_post();
             //Do some stuff...

It works perfect. It displays 30 posts but how do I access second page?

I tried:
- http://example.com/?paged=2 (server's 404 error)
- http://example.com/page/2 (redirects to homepage and removes /page/2 part)
- <?php wp_link_pages(); ?> (returns nothing)

I have default permalinks. What am I doing wrong? How do I access second page of posts?

  • have you tried if ($wp_query->have_posts()) : instead of if( have_posts() ) :
    – Amit Kosti
    Jul 28, 2012 at 20:45
  • @amit Yes, it didn't help and didn't change anything. That if statement was returning true all the time and while takes posts from correct loop.
    – Paul
    Jul 28, 2012 at 21:15
  • hey, http://example.com/portfolio/?paged=2 works ?
    – Amit Kosti
    Jul 28, 2012 at 22:02
  • @amit Nope. 404. I think that my loop is just wrong and I'll start from scratch (but first read more about loops).
    – Paul
    Jul 28, 2012 at 22:28

2 Answers 2


If you put the following after the while loop, does it link to anything?

<?php next_posts_link('&laquo; previous posts');
    if (get_next_posts_link() && get_previous_posts_link())
        echo ' | ';
    previous_posts_link('next posts &raquo;');
  • It links to exactly this (only previous link shows up): http://example.com/?paged=2 But when I enter that link I'm redirected to http://example.com/. I have no redirection plugins and clean .htaccess on this server.
    – Paul
    Jul 28, 2012 at 18:23
  • I know why it was redirecting me to homepage. Because 404.php has redirect. And the content of that paged=2 link is: 404 Not Found - http://example.com/?paged=2. So, my question should be "why is it returning 404?"
    – Paul
    Jul 28, 2012 at 18:37
  • Are the two URL fields in general settings correct?
    – ivvi
    Jul 28, 2012 at 18:39
  • Yes, it's fresh installation of WordPress. I already tried different permalinks structures without luck. It's always 404 - not found when I enter second page. Can it be something wrong with that loop in index.php perhaps? It gives me this notice: Undefined property: WP_Query::$post for instance.
    – Paul
    Jul 28, 2012 at 18:43
  • EDIT: resolved this error (it was because of some other undefined variable as argument) but pagination error still remains.
    – Paul
    Jul 28, 2012 at 18:49

I would recommend against using index.php as it also gets used for other archives should the more specific templates not exist. For the frontpage, use frontpage.php or home.php

Also if you're only displaying your custom query, I'd advise you use the pre_get_posts hook instead of throwing away the main query and presenting your own. This will let you use the main loop and sidestep the entire pagination issue

edit: notice you have if(have_posts), this checks if the main query has posts, not if your custom query has posts. Change it to $wp_query->have_posts instead

  • Neither home.php nor front-page.php work but my-template.php works (as page template). I move current (main) query to $temp but it seems this might be problematic.
    – Paul
    Jul 28, 2012 at 19:22
  • I don't understand what you mean by moving the main query to $temp, you don't 'move' the main query
    – Tom J Nowell
    Jul 28, 2012 at 19:24
  • It's like $temp = $wp_query; $wp_query = null; and then back after my one is finished.
    – Paul
    Jul 28, 2012 at 19:25
  • that's unnecessary though, just do wp_reset_postdata when you're finished with your WP_Query object, you should never have to do that with $wp_query, there's no need for it, and WP_Query doesn't modify $wp_query anyway, it modifies $post
    – Tom J Nowell
    Jul 28, 2012 at 19:26
  • 2
    Read this: slideshare.net/andrewnacin/…
    – Tom J Nowell
    Jul 28, 2012 at 19:27

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.