First of, change your getPostsCount function to this
function getPostsCount () {
return $this->db->count_all('posts');
}
The way you're doing it now is wasting time/memory/cpu & code lines for nothing.
Second thing, use a left/inner join to get other data rather than throwing a bunch of queries in a foreach statement (this is WRONG).
If you still need help with joining things, show table structures to get more help.
I think this tiny change, will make big difference.
EDIT:
After Providing more info, here's your query with a join to users (as it's not clear how your categories works, I am not including it).
function getAllPosts($limit = 0, $start = 0) {
$q = $this->db->select('p.*, u.nickname')
->from('posts p')
->join('users u', 'p.user_id = u.id', 'left')
->limit($limit, $start)
->get();
return $q->result_array();
}
This will return posts with the nickname of the user, as for the category it's not clear how you're storing them nor what explode categories is doing, If you're storing them in comma-separated field, you can use a single query to get all categories using ->where_in('id', array_of_ids);
You'll need to go through the manual to get more help on how to do things: http://ellislab.com/codeigniter/user-guide/