$this->db->select('*, COUNT(category_id) as rows'); $this->db->from('posts'); $this->db->group_by('category_id'); $this->db->order_by('rows','DESC'); $query = $this->db->get();