+ $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;
+}
+
+/**
+ * Get adjacent post relational link.
+ *
+ * Can either be next or previous post relational link.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link 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 display link to previous or next post. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
+ if ( $previous && is_attachment() && $post = get_post() )
+ $post = get_post( $post->post_parent );
+ else
+ $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
+
+ if ( empty( $post ) )
+ return;
+
+ $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
+
+ if ( empty( $post_title ) )
+ $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
+
+ $date = mysql2date( get_option( 'date_format' ), $post->post_date );
+
+ $title = str_replace( '%title', $post_title, $title );
+ $title = str_replace( '%date', $date, $title );
+
+ $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
+ $link .= esc_attr( $title );
+ $link .= "' href='" . get_permalink( $post ) . "' />\n";
+
+ $adjacent = $previous ? 'previous' : 'next';
+
+ /**
+ * Filter the adjacent post relational link.
+ *
+ * The dynamic portion of the hook name, $adjacent, refers to the type
+ * of adjacency, 'next' or 'previous'.
+ *
+ * @since 2.8.0
+ *
+ * @param string $link The relational link.
+ */
+ return apply_filters( "{$adjacent}_post_rel_link", $link );
+}
+
+/**
+ * Display relational links for the posts adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
+}
+
+/**
+ * Display relational links for the posts adjacent to the current post for single post pages.
+ *
+ * This is meant to be attached to actions like 'wp_head'. Do not call this directly in plugins or theme templates.
+ * @since 3.0.0
+ *
+ */
+function adjacent_posts_rel_link_wp_head() {
+ if ( !is_singular() || is_attachment() )
+ return;
+ adjacent_posts_rel_link();
+}
+
+/**
+ * Display relational link for the next post adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, false, $taxonomy );
+}
+
+/**
+ * Display relational link for the previous post adjacent to the current post.
+ *
+ * @since 2.8.0
+ *
+ * @param string $title Optional. Link title format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, true, $taxonomy );
+}
+
+/**
+ * Retrieve boundary post.
+ *
+ * Boundary being either the first or last post by publish date within the constraints specified
+ * by $in_same_term or $excluded_terms.
+ *
+ * @since 2.8.0
+ *
+ * @param bool $in_same_term Optional. Whether returned 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 $start Optional. Whether to retrieve first or last post.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return mixed Array containing the boundary post object if successful, null otherwise.
+ */
+function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
+ $post = get_post();
+ if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
+ return null;
+
+ $query_args = array(
+ 'posts_per_page' => 1,
+ 'order' => $start ? 'ASC' : 'DESC',
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false
+ );
+
+ $term_array = array();
+
+ if ( ! is_array( $excluded_terms ) ) {
+ if ( ! empty( $excluded_terms ) )
+ $excluded_terms = explode( ',', $excluded_terms );
+ else
+ $excluded_terms = array();
+ }
+
+ if ( $in_same_term || ! empty( $excluded_terms ) ) {
+ if ( $in_same_term )
+ $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
+
+ if ( ! empty( $excluded_terms ) ) {
+ $excluded_terms = array_map( 'intval', $excluded_terms );
+ $excluded_terms = array_diff( $excluded_terms, $term_array );
+
+ $inverse_terms = array();
+ foreach ( $excluded_terms as $excluded_term )
+ $inverse_terms[] = $excluded_term * -1;
+ $excluded_terms = $inverse_terms;
+ }
+
+ $query_args[ 'tax_query' ] = array( array(
+ 'taxonomy' => $taxonomy,
+ 'terms' => array_merge( $term_array, $excluded_terms )
+ ) );
+ }
+
+ return get_posts( $query_args );
+}
+
+/*
+ * Get previous post link that is adjacent to the current post.
+ *
+ * @since 3.7.0
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ * @return string
+ */
+function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
+}
+
+/**
+ * Display previous post link that is adjacent to the current post.
+ *
+ * @since 1.5.0
+ * @see get_previous_post_link()
+ *
+ * @param string $format Optional. Link anchor format.
+ * @param string $link Optional. Link permalink format.
+ * @param bool $in_same_term Optional. Whether link should be in a same taxonomy term.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
+ * @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default 'category'.
+ */
+function previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );