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.

So I wrote the following to create a transient:

$wp_object_i_created = new WP_Query('cat=1');
set_transient('category_one_posts', $wp_object_i_created, 60*60*1);

Ok so then I decided "Ya know lets walk through this and spit out the posts."

So I did the following:

if(false !== get_transient('category_one_posts')){
    $posts_inside = get_transient('category_one_posts');
    if($posts_inside->have_posts()){
        while($posts_inside->have_posts()){
            $posts_inside->the_post();
            // Display your posts in some maner.
        }
        echo get_next_posts_link('« Older Entries', $max_pages);
        echo get_previous_posts_link('Newer Entries »', $max_pages);
    }
 }

Now the pagination works - here's the catch - It works in a really failed way. So I have it set to display 5 posts per page.

So if I go to my link: http://localhost/wordpress/?page_id=1667 I see my 5 posts and at the bottom my two links - one for older, one for newer. Woot. But if I click on the button "Older Entries" to go back, so my link now looks like: http://localhost/wordpress/?page_id=1667&paged=2 I see that were on page two, I can go one page forward and so on - BUT the posts are the same, they never updated.

Now regular queries done to get specific posts based on specific parameters, or even just the regular WordPress loop - their pagination all work as expected, you go to page two, you get a different set of posts then that on page one - woot! - we work.

But transient based pagination does not work.

Why?

share|improve this question
    
Can you show us the WordPress loop code you are using when the get_transient('category_one_posts') call returns false (and paging works) and can you show us the code that defines $max_pages? –  Charles Clarkson Sep 23 '13 at 3:47
add comment

1 Answer

up vote 0 down vote accepted

Short answer: You are making the same query no matter what page you are, but you're expecting a different result.

Long answer: The query should be different for second page, having paged=2, so your query (and transients) with pagination should look like this:

$paged_var = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

if ( get_transient( 'category_one_posts-' . $paged_var ) == false ) {
    $posts_inside = new WP_Query(
        array(
             'cat' => 1,
             'paged' => $paged_var
        )
    );
    set_transient( 'category_one_posts-' . $paged_var, $posts_inside, 60*60*1 );
} else {
    $posts_inside = get_transient( 'category_one_posts-' . $paged_var );
}

if ( $posts_inside->have_posts() ) {
    while( $posts_inside->have_posts() ) {
        $posts_inside->the_post();
        // Display posts.
    }

    echo get_next_posts_link( __( 'Next', 'textdomain'), $posts_inside->max_num_pages );
    echo get_previous_posts_link( __( 'Previous', 'textdomain'), $posts_inside->max_num_pages );
}
share|improve this answer
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.