Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to paginate this query but I get no links for pagination at the end of the list of posts fetched. How can I fix it?

For further information, this query is run via a shortcode, so it might be placed on another page or post. So it might be the case that the post is already paginated, but within that post we then find this code that is getting some posts from the custom post type, and they need to be paginated as well.

    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    // Query to get all test items for display
    $test_items = new WP_Query( array(
        'post_type' => 'myplugin_test_item',
        'posts_per_page' => $settings['test_limit'], 
        'orderby'  => 'meta_value', 
        'meta_key' => 'myplugin_item_date', 
        'order' => 'DESC',
        'paged' => $paged,

    ) );

    // Globalize $wp_query
    global $wp_query;
    // Swap-hack
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = $test_items;        

    if( $test_items->have_posts() ) {
        while ( $test_items->have_posts() ) {                
            $test_items->the_post();
            $permalink = get_post_meta( get_the_ID(), 'myplugin_item_permalink', true );                
        }
        echo paginate_links();

        wp_reset_postdata();

    } else {
        echo 'No test items found';
    }

    $wp_query = null; 
    $wp_query = $temp;  // Reset
}
share|improve this question
possible duplicate of Pagination not working with custom loop – Chip Bennett Aug 27 '12 at 13:20
This question has been asked and answered several times already. – Chip Bennett Aug 27 '12 at 13:20
The other question is using PageNavi which I am not using. I also tried adapting it but it didn't work. – drtanz Aug 27 '12 at 14:05
What you use to output the pagination link is irrelevant. The underlying issue in all cases is that pagination is based on the default query, rather than on the custom query. – Chip Bennett Aug 27 '12 at 14:17
So it's impossible to paginate the custom query? – drtanz Aug 27 '12 at 14:48
show 3 more comments

closed as not a real question by toscho Oct 9 '12 at 22:13

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

2 Answers

Have you tried changing the get_query_var('page') with get_query_var('paged') ? This may change depending which file you are working on.

share|improve this answer
yes and still the same, no output at all from paginate_links() – drtanz Aug 27 '12 at 10:32

You could try adding the current & total pages as $args to the paginate_links function:

echo paginate_links( array(
    'current' => get_query_var('page') ? get_query_var('page') : 1,
    'total' => $test_items->max_num_pages
) );

Found here: http://codex.wordpress.org/Function_Reference/paginate_links#Examples

share|improve this answer
didn't help either – drtanz Aug 27 '12 at 13:59

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