cleaned-up pagination function with better WP internals and wrote tests for search