Skip to content

Instantly share code, notes, and snippets.

@brycejacobson
Last active February 27, 2020 15:11
Show Gist options
  • Save brycejacobson/f4372ff66f6f956cb25ab879b1152533 to your computer and use it in GitHub Desktop.
Save brycejacobson/f4372ff66f6f956cb25ab879b1152533 to your computer and use it in GitHub Desktop.
WordPress Post Navigation for Private Posts
<nav class="navigation post-navigation" role="navigation" aria-label="Posts">
<h2 class="screen-reader-text">Post Navigation</h2>
<div class="nav-links">
<?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(),
'rotonews', // The post type.
'private', // The post status.
'rotonews',
'private'
)
);
// var_dump( $results );
/**
* Check to see if a previous post was found
*
* @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
*/
function has_previous_post( $results ) {
return isset( $results[0] );
}
// If a previous post is found show the link.
if ( has_previous_post( $results ) == 1 ) {
?>
<div class="nav-previous">
<a href="<?php echo esc_url( get_the_permalink( $results[0]->ID ) ); ?>" rel="prev">
<span class="meta-nav" aria-hidden="true">Previous</span>
<span class="screen-reader-text">Previous post:</span>
<span class="post-title"><?php echo esc_attr( get_the_title( $results[0]->ID ) ); ?></span>
</a>
</div>
<?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(),
'rotonews', // The post type.
'private', // The post status.
'rotonews',
'private'
)
);
// var_dump( $results );
/**
* Check to see if a next post was found_posts
*
* @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
*/
function has_next_post( $results ) {
return isset( $results[0] );
}
// If a next post is found show the link.
if ( has_next_post( $results ) == 1 ) {
?>
<div class="nav-next">
<a href="<?php echo esc_url( get_the_permalink( $results[0]->ID ) ); ?>" rel="next">
<span class="meta-nav" aria-hidden="true">Next</span>
<span class="screen-reader-text">Next post:</span>
<span class="post-title"><?php echo esc_attr( get_the_title( $results[0]->ID ) ); ?></span>
</a>
</div>
<?php
}
?>
</div><!-- .nav-links -->
</nav><!-- .post-navigation -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment