Advertisement
thesufi

WP_Query with pagination in wp-admin pages (for plugin etc)

Feb 3rd, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
  2. $args = array(
  3.             'posts_per_page'    => 5,
  4.             'paged'         => $paged
  5.          );
  6.  
  7. $custom_query = new WP_Query( $args );
  8.  
  9. // Output custom query loop
  10. if ( $custom_query->have_posts() ) :
  11.     while ( $custom_query->have_posts() ) :
  12.         $custom_query->the_post();
  13.         // Loop output goes here
  14.     endwhile;
  15. endif;
  16. // Reset postdata
  17. wp_reset_postdata();
  18.  
  19. //wp_query object is not available in wp-admin. so we need to design custom pagination
  20. // Pagination
  21.     $max_pages = $custom_query->max_num_pages;
  22.     $nextpage = $paged + 1;
  23.     $previouspage = $paged - 1;
  24.    
  25.     if ($max_pages > $paged) {
  26.         echo '<a href="admin.php?page=plugin_page.php&paged='. $nextpage .'">Load More Posts</a>';
  27.     }
  28.     if ( $paged > 1 ) {
  29.         echo '<a href="admin.php?page=plugin_page.php&&paged='. $previouspage .'">Load Previous Posts</a>';
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement