Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I try to add a code to my wordpress blog show previous posts(in a previous page). I try this code but it don't work:

<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'offset' => 1,
    'cat' => -22,
    'paged'=>$paged
);
$recent = new WP_Query($args);
while($recent->have_posts()) : $recent->the_post();
// here some code to view
<?php endwhile; ?>
<div class="navigation"><p><?php posts_nav_link('&#8734;','next page','previous page'); ?></p></div>
<?php wp_reset_query(); ?>

it's doesn't work in http://mydomain.com/page/2

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You set offset param, so paged param is ignored ;)

From Codex:

offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged

If you want to ignore most recent post in this query, you can achieve it in two ways.

1. Custom pagination.

$ppp = get_option('posts_per_page');  // or any other method to get your posts_per_page

$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'offset' => 1 + ( $paged * $ppp ),
    'cat' => -22,
    'paged'=>$paged
);

2. Ignore first post

$most_recent_post_id = ...;  // get the ID of most recent post
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
    'post__not_in' => array($most_recent_post_id),
    'cat' => -22,
    'paged'=>$paged
);
share|improve this answer
    
In a page 1 I want to posts would be displayed from second post, What is solution? –  Ehsan Nov 13 '13 at 11:18
    
Hmm, you want to ignore most recent post, right? –  Krzysiek Dróżdż Nov 13 '13 at 11:20
    
yes, Exactly... –  Ehsan Nov 13 '13 at 11:30
    
thank you master –  Ehsan Nov 13 '13 at 11:49
add comment

Your Answer

 
discard

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.