Pagination in wordpress admin with wp-mvc

Pagination is very important functionality of any website.The wordpress provides many plugin to integrate beautiful pagination on your website,but some times you need to create your custom pagination based on your requirement.
I am using wp-mvc plugin to create plugin in wordpress based MVC framework. Wp MVC has inbuilt index action to create admin grid but if you need to customized grid, then you need to override index action of wp mvc.
So in this tutorial i will described how to create pagination based on mvc architecture.

You can also check other tutorial of wordpress,

There are following steps need to create pagination:

Step 1: We will override index action in controller file(testController.php).

public function index() {
		$post = isset($_REQUEST) ? $_REQUEST: array();
					'page' => isset($post['p']) ? intval($post['p']) : 1,
					'rp' => isset($post['rp']) ? intval($post['rp']) : 2,
					'id' => isset($this->params['id']) ? $this->params['id'] : 0
		$this->set('results', $data);
In action we will send each time page number/record_pre-page parameters to model and we will get result set as a response.

Step 2: Fetch data from model based on limit and offset parameters (model.php).

	function fetchSettings($options){
		global $wpdb;
		$collection = array();
		$data = array();
		$page = ($options['page'] > 1) ? $options['page'] : 1;
		$offset = $options['rp'];
		$limit = ($page-1)*$offset;
		$sql = "SELECT
					st.constant as setting_key,st.SID
					tbl_settings as st";
		if(!empty($options['id'])) {
			$sql .= " WHERE st.SID = '". $options['id'] ."'";
		$sql .= " LIMIT ". $limit .",".  $offset;

		$count = "SELECT
		$collection['results'] = $wpdb->get_results($sql, ARRAY_A);
		$collection['total'] = $wpdb->get_var($count)/$options['rp'];

		return $collection;


Step 3: Finally we will add pagination nav bar into view file(view.php).

$base, // the base URL, including query arg 'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p" 'prev_text' => __('« Previous'), // text for previous page 'next_text' => __('Next »'), // text for next page 'total' => $results['total'], // the total number of pages we have 'current' => isset($_REQUEST['p']) ? intval($_REQUEST['p']) : 1, // the current page 'end_size' => 1, 'mid_size' => 5, )); echo $pagination; ?>

  • Prabhu Bakiyaraj Chelladurai

    Your post is exactly what i want. But,it could be more clear about the steps you have given here.

    Step 1 is ok, since you have given the function name as index(), and anyone can understand or atleast they can find it by navigating files one by one.

    But what about step 2 and 3? where to put those codes? I dont understand since the function is user defined and i dont know where to place those codes…

    I am very new to WP, PHP, WP MVC etc.

    If you could make it clear, it will be much helpful.


    • You need to put step2 code into model file and step3 into view file where you are showing grid and want to show pagination.