WordPress pagination on custom script

The question:

I am using WP for my RealEstate project where all properties are pulled from a custom table (wp_listing) and displayed in a custom page template (page-realestate.php).

I know WordPress has built-in pagination methods to deal with paged content (pages, posts lists). I want to use that same pagination methods for the list of properties I am fetching from my custom DB table.

Is this possible? How?


Code is something like:

echo '<ul>';
$properties = $listing->getProperties();
for($i = 0; $i < count($properties) ; $i++) {
    echo '<li>' . $properties[$i]['address'] . '</li>';
echo '</ul>';

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

It is unclear exactly which “built-in pagination methods” you are looking for. WordPress uses several.

If you are looking for the next_posts_links and previous_posts_link functionality, write your own function as those built-in functions depend upon the $wp_query global as you can see in the source. I doubt it is worth the time and effort to try to hack that to work with data from your custom table.

If you want pagination as you see at the bottom of the admin edit post screen, the built-in function paginate_links is generic. So long as you pass in the proper values you can paginate your own data without a problem. I have done it, and it works well, but you have to be very careful with all the parameters. Based on the information in your question, I don’t think I can write the code you need. Plus, I am not even sure if that is what you are looking for.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment