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?

  • What do the paginated URLs look like? Have you verified that paged is set? – Milo Jan 20 '15 at 18:18
  • @Milo How do I verify if paged is set? The URL looks like: /wp-admin/admin.php?page=plugin_page.php&paged=2 – Christine Cooper Jan 20 '15 at 18:23
  • you could echo get_query_var('paged') to see what it outputs. I'll guess that there is no $wp_query object on your plugin page, so get_query_var won't return anything. you could try $_GET['paged'] instead. – Milo Jan 20 '15 at 18:27
  • @Milo echo $paged did not return anything. I did try: "$paged = ( $_GET['paged'] ) ? $_GET['paged'] : 1;" -- it loads up page 2 correctly, but doesn't go any further, even though there are tons of posts left. Why? We are getting to a solution though. :) – Christine Cooper Jan 20 '15 at 18:38
  • 1
    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. – Milo Jan 21 '15 at 16:31
up vote 3 down vote accepted

Milo noted that there is no $wp_query object in wp-admin page, so we can get $paged via:

$paged = ( $_GET['paged'] ) ? $_GET['paged'] : 1;

Now that we have $paged, we can code our own pagination. I will demonstrate how in its very simplest form.

First let's get maximum pagination pages:

$max_pages = $the_query->max_num_pages;

Then calculate the next page:

$nextpage = $paged + 1;

Finally, let's create our pagination link. We do a basic if statement checking if $max_pages is greater than $paged:

if ($max_pages > $paged) {
    echo '<a href="admin.php?page=plugin-page.php&paged='. $nextpage .'">Load More Topics</a>';
}

It is as simple as that.

Update

To enable previous page you can simply add:

$prevpage = max( ($paged - 1), 0 ); //max() will discard any negative value
if ($prevpage !== 0) {
   echo '<a href="admin.php?page=plugin-page.php&paged='. $prevpage .'">Previous page</a>';
}
  • 1
    Thanks Christine Cooper, that helped me a lot. I figured out to enable the 'Previous page' link, so I edited the question and added it to upvote the effort. :) – Mayeenul Islam Apr 21 '16 at 6:51
  • @MayeenulIslam Very nice, thanks for the addition! – Christine Cooper Apr 21 '16 at 10:24

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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