Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created December 6, 2018 17:29
Show Gist options
  • Select an option

  • Save tommcfarlin/763a68936f225834bba671a3d0a8b9f6 to your computer and use it in GitHub Desktop.

Select an option

Save tommcfarlin/763a68936f225834bba671a3d0a8b9f6 to your computer and use it in GitHub Desktop.
[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