• Hi all,

    I setup this code for custom querying different categories on a particular page, however I tried to add pagination, but it just keeps showing the same blog posts over and over again.

    <?php query_posts('cat=153,155,152,154,161&posts_per_page=5&offset=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="blog-query-entry">
    <div class="lc">
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large'); ?></a>
    </div>
    <div class="rc">
    <p class="post_cats"><?php the_category(' '); ?></p>
    
    <h2 class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <span class="post_date"><?php the_date(); ?></span>
    <span class="post_author"><?php the_author(); ?></span>
    </div>
    </div>
    <?php endwhile; endif; ?>
    
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php wp_reset_query(); ?>

    I’m not sure if I’m doing this right. I’m still new to PHP.

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must add the ‘paged’ argument to your query_posts() call. Try changing this:

    <?php query_posts('cat=153,155,152,154,161&posts_per_page=5&offset=1'); ?>

    to this:

    <?php if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    query_posts('cat=153,155,152,154,161&posts_per_page=5&offset=1&paged=' . $paged); ?>

    Thread Starter robertallen

    (@robertallen)

    Hmm, I’m still getting the same issue.

    <?php if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    query_posts('cat=153,155,152,154,161&posts_per_page=5&offset=1&paged=' . $paged);
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="blog-query-entry">
    <div class="lc">
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large'); ?></a>
    </div>
    <div class="rc">
    <p class="post_cats"><?php the_category(' '); ?></p>
    
    <h2 class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <span class="post_date"><?php the_date(); ?></span>
    <span class="post_author"><?php the_author(); ?></span>
    </div>
    </div>
    <?php endwhile; endif; ?>
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php wp_reset_query(); ?>

    You can see the problem at amplifinity.wpengine.com/blog/

    I wonder if it has something to do with the top section…

    I have a query for the first area of the blog…

    <?php query_posts('cat=153,155,152,154,161&posts_per_page=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <p class="post_cats"><?php the_category(' '); ?></p>
    
    <h2 class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large'); ?></a>
    
    <div class="blog-tl-author-date">
    <span class="post_date"><?php the_date(); ?></span>
    <span class="post_author"><?php the_author(); ?></span>
    </div>
    <div class="blog-tl-excerpt">
       <?php the_excerpt(); ?>
    
    <a class="excerpt_read_more" href="<?php the_permalink(); ?>">Continue Reading</a>
    </div>
    <?php endwhile; endif; ?>
    <?php wp_reset_query(); ?>

    Basically the client wanted a large feature blog of specific categories, and then the rest underneath of specific categories. I must be missing something.

    Don’t know if it makes a difference, but try putting your pagination between the endwhile and endif at the end of the loop, like this:

    <?php endwhile; ?>
       <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
       <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    Use this along with the code to set $paged.

    Thread Starter robertallen

    (@robertallen)

    That helped a bit for sure, thank you…

    But I noticed the problem. I got rid of &offset=1 in it and now it works.

    So the pagination doesn’t work when offset is around. Hmm.

    Thread Starter robertallen

    (@robertallen)

    Also once it gets to the last page it still displays Older Posts, then goes to a 404 page.

    Do you have another loop after this one?

    Thread Starter robertallen

    (@robertallen)

    No, just this one. But the top has one loop, but it doesn’t have an offset.

    this is the top blog post

    <?php query_posts('cat=153,155,152,154,161&posts_per_page=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <p class="post_cats"><?php the_category(' '); ?></p>
    
    <h2 class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large'); ?></a>
    
    <div class="blog-tl-author-date">
    <span class="post_date"><?php the_date(); ?></span>
    <span class="post_author"><?php the_author(); ?></span>
    </div>
    <div class="blog-tl-excerpt">
       <?php the_excerpt(); ?>
    
    <a class="excerpt_read_more" href="<?php the_permalink(); ?>">Continue Reading</a>
    </div>
    <?php endwhile; endif; ?>
    <?php wp_reset_query(); ?>

    This is the bottom blog posts…

    <?php if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    query_posts('cat=153,155,152,154,161&posts_per_page=5&paged=' . $paged);
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="blog-query-entry">
    <div class="lc">
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail($post_id, 'large'); ?></a>
    </div>
    <div class="rc">
    <p class="post_cats"><?php the_category(' '); ?></p>
    
    <h2 class="headline"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    
    <span class="post_date"><?php the_date(); ?></span>
    <span class="post_author"><?php the_author(); ?></span>
    </div>
    </div>
    <?php endwhile; ?>
       <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
       <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

    I took out the offset and noticed aside from the last older post going to page 8 and.

    My hope is to be able to keep the offset in there and for it not to go to a 404 on the last page.

    So I guess it won’t display older posts when it’s done querying?

    Couple of questions.

    1. Is this a template that is used in a Page?

    2. Can you try taking out the posts_per_page parameter on the second query?

    Thread Starter robertallen

    (@robertallen)

    First off, thank you so much for your help on this. I really appreciate it.

    It’s actually a template used int he Blog (Home). We created this from a blank Thesis skin.

    We took out the posts_per_page and that seemed to fix the Older Posts issue, but when I added offset=1 back in it still only shows the same posts on every pagination.

    I am guessing that you want to use the offset to prevent the featured post from showing up in the second loop. If that is the case, save the ID in the first loop and use it in the post__not_in argument in the second loop.

    Saving the ID:

    <?php query_posts('cat=153,155,152,154,161&posts_per_page=1'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php $featured_id = get_the_ID(); ?>
    <p class="post_cats"><?php the_category(' '); ?></p>

    Using post__not_in in the second loop:

    <?php if (!$paged = get_query_var('paged')) {
       if (!$paged = get_query_var('page')) {
          $paged = 1;
       }
    }
    $args = array(
       'cat' => '153,155,152,154,161',
       'paged' => $paged,
       'post__not_in' => array( $featured_id ),
    );
    query_posts( $args );
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    The reason you can’t use offset with pagination is that MySQL uses it to handle the pagination itself. Here are three main steps that are used in a query:

    1. Select all records that match the criteria.
    2. Sort the records if required.
    3. Select a subset of the records to return if required.

    The subset of records is chosen based on the LIMIT clause of the query. The LIMIT clause uses 2 parameters: the number of records to return, and an offset into the total selected set.

    The number of records is determined in WP by the posts_per_page value.

    The offset is calculated as (paged – 1) * number of records.

    So, if posts_per_page is 10, and paged = 3, the clause would be: LIMIT (3 – 1) * 10, 10

    LIMIT 20, 10

    So if you specify your own offset, WP can’t page.

    Thread Starter robertallen

    (@robertallen)

    Thank you so much for all your help with this vtxyzzy. We decided to throw a little workaround, while it’s not ideal, it got the job done.

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Custom Query with Pagination Problems’ is closed to new replies.