I've created a custom plugin page. On this page, I list posts using wp_query(). It works fine. I want to add pagination, but it doesn't work, even when I'm using the code provided within codex:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'posts_per_page=2&paged=' . $paged );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
// clean up after the query and pagination
wp_reset_postdata();
else:
echo 'Sorry, no posts matched your criteria.';
endif; ?>
When clicking on the pagination link, it loads up new page with the same posts. Why is this not working in the wp-admin area?
paged
is set?echo get_query_var('paged')
to see what it outputs. I'll guess that there is no$wp_query
object on your plugin page, soget_query_var
won't return anything. you could try$_GET['paged']
instead.next_posts_link
is a front end function. it relies on the$paged
global, which is probably not correctly set on some admin pages. you know the number of pages ($the_query->max_num_pages
), just output your own pagination links.