WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The loop below is one of several on a Custom Page template, but it's the only loop that I'm trying to implement Pagination with.

Once I have Pagination working for this one loop, I'll be implementing InfiniteScroll which I'm sure will be way easier than installing it first...

I've read miles of pages but my brain just isn't finding the right information to get this to work yet.

Designing themes is one thing, but engineering loops is still something I struggle with.

    <div class="thirdLeft">
            global $paged;
            $curpage = $paged ? $paged : 1;
            $args = array(
                'post_type' => 'location',
                'orderby' => 'post_date',
                'posts_per_page' => 5,
                'paged' => $paged
            $first_query = new WP_Query('cat=2,-20&showposts=18&offset=2');
            while($first_query->have_posts()) : $first_query->the_post();

                <?php get_template_part( 'content', 'single' ); ?>  
            <?php endwhile; ?>


After scouring the web, I found this great tut: http://weblogtoolscollection.com/archives/2008/04/19/paging-and-custom-wordpress-loops/

Formatting my loop above to work and get the pagination it deserves, an updated version looks like this:

(not much changed from the tut, aside from custom categories)

    <?php add_filter('post_limits', 'my_post_limit'); ?>
    <div class="thirdLeft">
                global $myOffset;
                $myOffset = 2;
                $temp = $wp_query;
                $wp_query= null;
                $wp_query = new WP_Query();
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div class="longLoopNav">
          <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
          <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
        <?php $wp_query = null; $wp_query = $temp;?>
        <?php remove_filter('post_limits', 'my_post_limit'); ?>

Now to figure out Infinite Scroll to load more posts into this loop with Ajax

share|improve this question
Please look at the related posts on the right. The standard pagination functions are not meant to work with, and do not work well with secondary queries. – s_ha_dum Aug 1 '13 at 22:40
@s_ha_dum I've read handfuls of them and most are trying to implement this and getting results but not much mention of multiple loops from what I've seen so far. I'll keep looking. Can you recommend any methods to help the progress of the question? – canacast Aug 1 '13 at 22:43
Related: wordpress.stackexchange.com/a/77666/21376 – s_ha_dum Aug 1 '13 at 23:11
Related: wordpress.stackexchange.com/a/106997/21376 – s_ha_dum Aug 1 '13 at 23:15
Related: wordpress.stackexchange.com/a/104488/21376 – s_ha_dum Aug 1 '13 at 23:16

Why can't you write custom pagination?

function theme_get_next_posts_link($query, $label = null, $max_page = 0)    
        global $paged;      
        if(!$max_page) $max_page = $query->max_num_pages;       
        if(!$paged) $paged = 1;     
        $nextpage = intval($paged) + 1;     
        if(null === $label) $label = "Older Posts";     
        if(!is_single() && $nextpage <= $max_page):         
            $attr = apply_filters( 'next_posts_link_attributes', '' );          
            echo '<a class="page-l" href="' . next_posts( $max_page, false ) . "\" $attr>" . preg_replace('/&([^#])(?![a-z]{1,8};)/i', '&$1', $label) . '</a>';     

    function theme_get_previous_posts_link($label = null)   
        global $paged;      
        if(null === $label) $label = "Newer Posts";     
        if(!is_single() && $paged > 1):         
            $attr = apply_filters( 'previous_posts_link_attributes', '' );          
            echo '<a class="page-r" href="' . previous_posts( false ) . "\" $attr>". preg_replace( '/&([^#])(?![a-z]{1,8};)/', '&$1', $label ) .'</a>';     

Using: <?php theme_get_next_posts_link($first_query); ?> for Older Posts <?php theme_get_previous_posts_link(); ?> for Newer Posts

In your code:

    global $paged;
    $args = array(
        'post_status'=> 'publish',
        'paged'=> $paged,
        'ignore_sticky_posts'=> 1,
        'posts_per_page'=>10 // -1 - all
    $fp_query = new WP_Query( $args );
    if ( $fp_query->have_posts() ):
    while ($fp_query->have_posts()) : $fp_query->the_post();
<?php endwhile;?>
    <div class="longLoopNav">
        <div class="alignleft"><?php theme_get_next_posts_link($fp_query); ?></div>
        <div class="alignright"><?php theme_get_previous_posts_link(); ?></div>
<?php endif; ?>
share|improve this answer
I'm still relatively new to working with custom queries, but this looks really thorough. Would you be able to guide me a little with a run through of what you're doing here? – canacast Aug 2 '13 at 7:22
I added example of using in my post. – Vladimir Aug 2 '13 at 9:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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