Pagination in plugin's backend

I am displaying a list of posts in the plugin backend but I can't seem to get the pagination to work. I used the standard approach of getting the "paged" variable and then passing it to get_posts()'s arguments, but whenever I visit my custom backend page or subpage and add the page=2 variable I get that permissions error about not having enough of them to visit that page.

Is there a special way to apply pagination in the backend?

Topic add-menu-page admin-menu php pagination Wordpress

Category Web


In the admin back-end of a plugin, you can't use the query_vars hook and the get_query_var built in function since the page itself is a query parameter. To get around this, I simply add a new query parameter to the end of your plugin page link called, '&pagenum=#" and I pull it out manually using:

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

There might be a better way of doing this, but I haven't found one that requires less effort.

Here is what the plugin url might look like:

admin.php?page=[plugin-dir]%2F[plugin-slug].php&pagenum=1

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.