Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Display a pagination for custom admin panel query mostly with WordPress styling.
<?php
$posts_per_page = get_option( 'posts_per_page );
$paged = $_GET['paged'] ? $_GET['paged'] : 1;
$tutsnano_page_query = new WP_Query(
array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $paged
)
);
while( $tutsnano_page_query->have_posts() ) : $tutsnano_page_query->the_post();
//displaying the query data as per choice
endwhile; wp_reset_postdata();
//display the pagination here
tutsnano_admin_query_pagination( $tutsnano_page_query, $posts_per_page, 'admin.php?page=my_page_slug' );
<?php
$posts_per_page = get_option( 'posts_per_page );
$paged = $_GET['paged'] ? $_GET['paged'] : 1;
$tutsnano_page_query = new WP_Query(
array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => $posts_per_page,
'paged' => $paged
)
);
<?php
/**
* Admin query navigation.
*
* Display a pagination for custom admin panel query mostly
* with WordPress styling.
*
* @author Christine Cooper
* @author Mayeenul Islam
* @link http://wordpress.stackexchange.com/a/175649/22728
* @link http://tuts.nanodesignsbd.com/en/paginate-custom-wp-query-in-admin-end/
*
* @param string $query Query object.
* @param integer $items_per_page Posts per page.
* @param string $page_path The page where query is executed.
*/
function tutsnano_admin_query_pagination( $query = '', $items_per_page = '', $page_path = '' ) {
if( empty($query) )
return 'Query must be set';
$total_pages = (int) $query->max_num_pages;
//as get_query_var() won't work in admin end
$paged = $_GET['paged'] ? (int) $_GET['paged'] : 1;
$nextpage = $paged + 1;
$prevpage = max( ($paged - 1), 0 ); //max() to discard any negative value
//assumed, we're using the default posts_per_page value in our query too
$items_per_page = empty($items_per_page) ? (int) get_option( 'posts_per_page' ) : (int) $items_per_page;
$lastpage = ceil( $query->found_posts / $items_per_page );
if( empty($page_path) )
return _e( 'Admin path is not set' );
//adding 'paged' parameter to page_path
$next_page_path = add_query_arg( 'paged', $nextpage, $page_path );
$prev_page_path = add_query_arg( 'paged', $prevpage, $page_path );
$lastpage_path = add_query_arg( 'paged', $lastpage, $page_path );
echo '<div class="tablenav bottom">';
echo '<div class="alignleft">';
//Display the 'Previous' buttons
if( $prevpage !== 0 ) {
echo '<a class="button button-default" title="First page" href="'. $page_path .'">&laquo; <span class="screen-reader-text">First page</span></a> ';
echo '<a class="button button-primary" title="Previous page" href="'. $prev_page_path .'">&lsaquo; <span class="screen-reader-text">Previous page</span></a> ';
}
//Display current page number
if( $paged !== 1 && $paged !== $total_pages ) {
echo '<span class="screen-reader-text">Current Page</span>';
echo '<span id="this-page">'. $paged .'</span>';
}
//Display the 'Next' buttons
if ($total_pages > $paged) {
echo ' <a class="button button-primary" title="Next page" href="'. $next_page_path .'"><span class="screen-reader-text">Next page</span> &rsaquo;</a>';
echo ' <a class="button button-default" title="Last page" href="'. $lastpage_path .'"><span class="screen-reader-text">Last page</span> &raquo;</a>';
}
echo '</div>';
echo '</div>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment