I have the following problem. In my custom menu (wp_nav_menu) I added a category. This category uses the template category.php. Now I have multiple categories that are shown on the page. My problem is that the pagination doesn't work. It shows the link en all but page/2/ display's an 'page not found'-page.

I have no idea what I'm doing wrong.

See my code:

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // allow for pagination
        $getcat = query_posts('category_name=array("cat1", "cat2", "cat3","cat4","cat5","cat6")'); 
        $args = array (
            'posts_per_page' => 5,
            'paged' => $paged
    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <div class="some_class"><?php the_excerpt(); ?></div>
    <?php endwhile; ?>
            <div class="pagination">
                <div class="ex_previous"><?php previous_posts_link('&laquo; recent', 0); ?></div>
                <div class="ex_next"><?php next_posts_link('older &raquo;', 0); ?></div>
    <?php else : ?>
    <?php endif; ?> 

Hope anyone can tell me where I went wrong



You have a few things wrong with you query. I will explain as I go along

Firstly, you should never use query_posts to construct custom queries. This is not just my emphasis, but the codex as well. The one big problem with query_posts is, it many circumstances, pagination fails

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Secondly, the parameter that you've used category_name excepts a string, not an array.

Thirdly, you've run two occurences of query_posts where only one is actually needed. This is also why your pagination out right fails

Fouthly, your next_posts_link $max_pages parameter should be set for custom queries, otherwise it will fail

OK, to come to the business end, you need to construct you custom query with WP_Query, keeping in mind some of the points I've raised above.

    $paged = (get_query_var('paged')) ?  get_query_var('paged') : 1; // this section is correct, allows pagination

 $args = array( //set-up arguments to pass to WP_Query
    'category_name' => 'string of category slugs, not name', // can use 'cat' parameter with string of category ID
    'posts_per_page' => 5,
    'paged' = $paged

$query = new WP_Query( $args); //add arguments back to WP_Query

if($query->have_posts()) :  while($query->have_posts()) : $query->the_post(); //start the loop
  <-----YOUR LOOP---->
     Remember next_posts_link should look something like 'next_posts_link( 'Older Entries', $query->max_num_pages );'
  • WOW. Nice detailed explanation. Thnx. It still doesn't work. I wrote a detailed answer but since I have no reputation over 10 I can't comment :-( So.... I have to wait 8 hours. – Interactive Jun 19 '14 at 10:20
  • Since I saw the link I get the idea there is some error here: I added the category 'news' to the menu. That category uses the template category.php the above code is used on category.php. Doesn't the pagination expects a page where only the posts from category 'news' are? M. – Interactive Jun 19 '14 at 10:21
  • What do you mean doesn't work. – Pieter Goosen Jun 19 '14 at 10:24
  • I think your question is wrongly worded. I think what you are looking for is when you click on a category, lets say news, you are taken to a page showing posts from the news category, and when you go to page two, the next set of posts are shown from the news category – Pieter Goosen Jun 19 '14 at 10:30
  • What I tried to explain in my previous post is that I need to list all the posts from multiple categories. But I realised that I added a category called news to my menu. The news category uses the category.php-template where the above code is used. I think I need to create a page-template that uses this code and a new page with the template – Interactive Jun 19 '14 at 11:20

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.