Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] WordPress Pagination: A Simple Utility (And Why)
<?php
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"
SELECT *
FROM $wpdb->posts
WHERE ID < (
SELECT ID
FROM $wpdb->posts
WHERE ID = %d
AND post_type = '%s'
AND post_status = '%s'
ORDER BY ID DESC
)
AND post_type = '%s'
AND post_status = '%s'
ORDER BY ID DESC
LIMIT 1
",
get_the_ID(),
'acme-custom-post-type',
'publish',
'acme-custom-post-type',
'publish'
)
);
<?php
global $wpdb;
$results = $wpdb->get_results(
$wpdb->prepare(
"
SELECT *
FROM $wpdb->posts
WHERE ID > (
SELECT ID
FROM $wpdb->posts
WHERE ID = %d
AND post_type = '%s'
AND post_status = '%s'
ORDER BY ID ASC
)
AND post_type = '%s'
AND post_status = '%s'
ORDER BY ID ASC
LIMIT 1
",
get_the_ID(),
'acme-custom-post-type',
'publish',
'acme-custom-post-type',
'publish'
)
);
<?php
/**
* @param array $results the results of the query to determined if there are past posts
*
* @return bool true if there is a previous post; otherwise, false
*/
private function hasPreviousPost($results)
{
return isset($results[0]);
}
<?php
/**
* @param array $results the results of the query to determined if there are future posts
*
* @return bool true if there is a next post; otherwise, false
*/
private function hasNextPost($results)
{
return isset($results[0]);
}
<?php
/**
* @param array $results the results of the array from which to retrieve the post ID
*
* @return string the ID of the post to which we're going to link
*/
private function getPostLink($results)
{
return get_the_permalink($results[0]->ID);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment