Skip to content

Instantly share code, notes, and snippets.

@aliciaduffy
Created August 8, 2014 12:52
Show Gist options
  • Save aliciaduffy/63d0910be215f162ec04 to your computer and use it in GitHub Desktop.
Save aliciaduffy/63d0910be215f162ec04 to your computer and use it in GitHub Desktop.
get_adjacent_posts()
<?php
/**
* Retrieve adjacent post.
*
* Can either be next or previous post.
*
* @since 2.5.0
*
* @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
* @param bool $previous Optional. Whether to retrieve previous post.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
* @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
*/
function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
global $wpdb;
if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
return null;
$current_post_date = $post->post_date;
$join = '';
$posts_in_ex_terms_sql = '';
if ( $in_same_term || ! empty( $excluded_terms ) ) {
$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
if ( $in_same_term ) {
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
return '';
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
if ( ! $term_array || is_wp_error( $term_array ) )
return '';
$join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
}
$posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
if ( ! empty( $excluded_terms ) ) {
if ( ! is_array( $excluded_terms ) ) {
// back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
if ( false !== strpos( $excluded_terms, ' and ' ) ) {
_deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
$excluded_terms = explode( ' and ', $excluded_terms );
} else {
$excluded_terms = explode( ',', $excluded_terms );
}
}
$excluded_terms = array_map( 'intval', $excluded_terms );
if ( ! empty( $term_array ) ) {
$excluded_terms = array_diff( $excluded_terms, $term_array );
$posts_in_ex_terms_sql = '';
}
if ( ! empty( $excluded_terms ) ) {
$posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
}
}
}
$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
/**
* Filter the JOIN clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, $adjacent, refers to the type
* of adjacency, 'next' or 'previous'.
*
* @since 2.5.0
*
* @param string $join The JOIN clause in the SQL.
* @param bool $in_same_term Whether post should be in a same taxonomy term.
* @param array $excluded_terms Array of excluded term IDs.
*/
$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
/**
* Filter the WHERE clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, $adjacent, refers to the type
* of adjacency, 'next' or 'previous'.
*
* @since 2.5.0
*
* @param string $where The WHERE clause in the SQL.
* @param bool $in_same_term Whether post should be in a same taxonomy term.
* @param array $excluded_terms Array of excluded term IDs.
*/
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
/**
* Filter the ORDER BY clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, $adjacent, refers to the type
* of adjacency, 'next' or 'previous'.
*
* @since 2.5.0
*
* @param string $order_by The ORDER BY clause in the SQL.
*/
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
$query_key = 'adjacent_post_' . md5( $query );
$result = wp_cache_get( $query_key, 'counts' );
if ( false !== $result ) {
if ( $result )
$result = get_post( $result );
return $result;
}
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = '';
wp_cache_set( $query_key, $result, 'counts' );
if ( $result )
$result = get_post( $result );
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment