<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[25959] trunk: Add a $taxonomy argument to each of the adjacent post functions.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/25959">25959</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-10-27 21:39:51 +0000 (Sun, 27 Oct 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add a $taxonomy argument to each of the adjacent post functions.
Each took an array of category (IDs) when to search. Those can now be term IDs and each function now has $taxonomy = 'category' as an optional argument.
Functions affected: get_previous_post(), get_next_post(), get_adjacent_post(), get_adjacent_post_rel_link(), adjacent_posts_rel_link(), next_post_rel_link(), prev_post_rel_link(), get_boundary_post(), get_previous_post_link(), previous_post_link(), get_next_post_link(), next_post_link(), get_adjacent_post_link(), adjacent_post_link().
props ethitter.
finally fixes <a href="http://core.trac.wordpress.org/ticket/17807">#17807</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeslinktemplatephp">trunk/src/wp-includes/link-template.php</a></li>
<li><a href="#trunktestsphpunittestslinkphp">trunk/tests/phpunit/tests/link.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeslinktemplatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/link-template.php (25958 => 25959)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/link-template.php 2013-10-27 21:13:55 UTC (rev 25958)
+++ trunk/src/wp-includes/link-template.php 2013-10-27 21:39:51 UTC (rev 25959)
</span><span class="lines">@@ -1091,12 +1091,13 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 1.5.0
</span><span class="cx"> *
</span><del>- * @param bool $in_same_cat Optional. Whether post should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
</del><ins>+ * @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 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.
</ins><span class="cx"> */
</span><del>-function get_previous_post($in_same_cat = false, $excluded_categories = '') {
- return get_adjacent_post($in_same_cat, $excluded_categories);
</del><ins>+function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1104,12 +1105,13 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 1.5.0
</span><span class="cx"> *
</span><del>- * @param bool $in_same_cat Optional. Whether post should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
</del><ins>+ * @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 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.
</ins><span class="cx"> */
</span><del>-function get_next_post($in_same_cat = false, $excluded_categories = '') {
- return get_adjacent_post($in_same_cat, $excluded_categories, false);
</del><ins>+function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1119,54 +1121,55 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.5.0
</span><span class="cx"> *
</span><del>- * @param bool $in_same_cat Optional. Whether post should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @param bool $previous Optional. Whether to retrieve previous post.
- * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
</del><ins>+ * @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.
</ins><span class="cx"> */
</span><del>-function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
</del><ins>+function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
</ins><span class="cx"> global $wpdb;
</span><span class="cx">
</span><del>- if ( ! $post = get_post() )
</del><ins>+ if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
</ins><span class="cx"> return null;
</span><span class="cx">
</span><span class="cx"> $current_post_date = $post->post_date;
</span><span class="cx">
</span><span class="cx"> $join = '';
</span><del>- $posts_in_ex_cats_sql = '';
- if ( $in_same_cat || ! empty( $excluded_categories ) ) {
</del><ins>+ $posts_in_ex_terms_sql = '';
+ if ( $in_same_term || ! empty( $excluded_terms ) ) {
</ins><span class="cx"> $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";
</span><span class="cx">
</span><del>- if ( $in_same_cat ) {
- if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
</del><ins>+ if ( $in_same_term ) {
+ if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
</ins><span class="cx"> return '';
</span><del>- $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
- if ( ! $cat_array || is_wp_error( $cat_array ) )
</del><ins>+ $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
+ if ( ! $term_array || is_wp_error( $term_array ) )
</ins><span class="cx"> return '';
</span><del>- $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
</del><ins>+ $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
- if ( ! empty( $excluded_categories ) ) {
- if ( ! is_array( $excluded_categories ) ) {
- // back-compat, $excluded_categories used to be IDs separated by " and "
- if ( strpos( $excluded_categories, ' and ' ) !== false ) {
- _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
- $excluded_categories = explode( ' and ', $excluded_categories );
</del><ins>+ $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 );
</ins><span class="cx"> } else {
</span><del>- $excluded_categories = explode( ',', $excluded_categories );
</del><ins>+ $excluded_terms = explode( ',', $excluded_terms );
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx">
</span><del>- $excluded_categories = array_map( 'intval', $excluded_categories );
</del><ins>+ $excluded_terms = array_map( 'intval', $excluded_terms );
</ins><span class="cx">
</span><del>- if ( ! empty( $cat_array ) ) {
- $excluded_categories = array_diff($excluded_categories, $cat_array);
- $posts_in_ex_cats_sql = '';
</del><ins>+ if ( ! empty( $term_array ) ) {
+ $excluded_terms = array_diff( $excluded_terms, $term_array );
+ $posts_in_ex_terms_sql = '';
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- if ( !empty($excluded_categories) ) {
- $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
</del><ins>+ 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 );
</ins><span class="cx"> }
</span><span class="cx"> }
</span><span class="cx"> }
</span><span class="lines">@@ -1175,13 +1178,13 @@
</span><span class="cx"> $op = $previous ? '<' : '>';
</span><span class="cx"> $order = $previous ? 'DESC' : 'ASC';
</span><span class="cx">
</span><del>- $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
- $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_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories );
</del><ins>+ $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
+ $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 );
</ins><span class="cx"> $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
</span><span class="cx">
</span><span class="cx"> $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
</span><del>- $query_key = 'adjacent_post_' . md5($query);
- $result = wp_cache_get($query_key, 'counts');
</del><ins>+ $query_key = 'adjacent_post_' . md5( $query );
+ $result = wp_cache_get( $query_key, 'counts' );
</ins><span class="cx"> if ( false !== $result ) {
</span><span class="cx"> if ( $result )
</span><span class="cx"> $result = get_post( $result );
</span><span class="lines">@@ -1192,7 +1195,7 @@
</span><span class="cx"> if ( null === $result )
</span><span class="cx"> $result = '';
</span><span class="cx">
</span><del>- wp_cache_set($query_key, $result, 'counts');
</del><ins>+ wp_cache_set( $query_key, $result, 'counts' );
</ins><span class="cx">
</span><span class="cx"> if ( $result )
</span><span class="cx"> $result = get_post( $result );
</span><span class="lines">@@ -1207,34 +1210,35 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.8.0
</span><span class="cx"> *
</span><del>- * @param string $title Optional. Link title format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
</del><ins>+ * @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'.
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>-function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
</del><ins>+function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
</ins><span class="cx"> if ( $previous && is_attachment() && $post = get_post() )
</span><span class="cx"> $post = get_post( $post->post_parent );
</span><span class="cx"> else
</span><del>- $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
</del><ins>+ $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
</ins><span class="cx">
</span><del>- if ( empty($post) )
</del><ins>+ if ( empty( $post ) )
</ins><span class="cx"> return;
</span><span class="cx">
</span><span class="cx"> $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) );
</span><span class="cx">
</span><span class="cx"> if ( empty( $post_title ) )
</span><del>- $post_title = $previous ? __('Previous Post') : __('Next Post');
</del><ins>+ $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
</ins><span class="cx">
</span><del>- $date = mysql2date(get_option('date_format'), $post->post_date);
</del><ins>+ $date = mysql2date( get_option( 'date_format' ), $post->post_date );
</ins><span class="cx">
</span><del>- $title = str_replace('%title', $post_title, $title);
- $title = str_replace('%date', $date, $title);
</del><ins>+ $title = str_replace( '%title', $post_title, $title );
+ $title = str_replace( '%date', $date, $title );
</ins><span class="cx">
</span><span class="cx"> $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
</span><span class="cx"> $link .= esc_attr( $title );
</span><del>- $link .= "' href='" . get_permalink($post) . "' />\n";
</del><ins>+ $link .= "' href='" . get_permalink( $post ) . "' />\n";
</ins><span class="cx">
</span><span class="cx"> $adjacent = $previous ? 'previous' : 'next';
</span><span class="cx"> return apply_filters( "{$adjacent}_post_rel_link", $link );
</span><span class="lines">@@ -1245,13 +1249,14 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.8.0
</span><span class="cx"> *
</span><del>- * @param string $title Optional. Link title format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> */
</span><del>-function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
- echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
- echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
</del><ins>+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 );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1272,12 +1277,13 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.8.0
</span><span class="cx"> *
</span><del>- * @param string $title Optional. Link title format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> */
</span><del>-function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
- echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
</del><ins>+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 );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1285,56 +1291,71 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 2.8.0
</span><span class="cx"> *
</span><del>- * @param string $title Optional. Link title format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> */
</span><del>-function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
- echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
</del><ins>+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 );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="cx"> * Retrieve boundary post.
</span><span class="cx"> *
</span><span class="cx"> * Boundary being either the first or last post by publish date within the constraints specified
</span><del>- * by $in_same_cat or $excluded_categories.
</del><ins>+ * by $in_same_term or $excluded_terms.
</ins><span class="cx"> *
</span><span class="cx"> * @since 2.8.0
</span><span class="cx"> *
</span><del>- * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @param bool $start Optional. Whether to retrieve first or last post.
</del><ins>+ * @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'.
</ins><span class="cx"> * @return object
</span><span class="cx"> */
</span><del>-function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
</del><ins>+function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
</ins><span class="cx"> $post = get_post();
</span><del>- if ( ! $post || ! is_single() || is_attachment() )
</del><ins>+ if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
</ins><span class="cx"> return null;
</span><span class="cx">
</span><del>- $cat_array = array();
- if( ! is_array( $excluded_categories ) )
- $excluded_categories = explode( ',', $excluded_categories );
</del><ins>+ $query_args = array(
+ 'posts_per_page' => 1,
+ 'order' => $start ? 'ASC' : 'DESC',
+ 'update_post_term_cache' => false,
+ 'update_post_meta_cache' => false
+ );
</ins><span class="cx">
</span><del>- if ( $in_same_cat || ! empty( $excluded_categories ) ) {
- if ( $in_same_cat )
- $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
</del><ins>+ $term_array = array();
</ins><span class="cx">
</span><del>- if ( ! empty( $excluded_categories ) ) {
- $excluded_categories = array_map( 'intval', $excluded_categories );
- $excluded_categories = array_diff( $excluded_categories, $cat_array );
</del><ins>+ if ( ! is_array( $excluded_terms ) ) {
+ if ( ! empty( $excluded_terms ) )
+ $excluded_terms = explode( ',', $excluded_terms );
+ else
+ $excluded_terms = array();
+ }
</ins><span class="cx">
</span><del>- $inverse_cats = array();
- foreach ( $excluded_categories as $excluded_category )
- $inverse_cats[] = $excluded_category * -1;
- $excluded_categories = $inverse_cats;
</del><ins>+ 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;
</ins><span class="cx"> }
</span><ins>+
+ $query_args[ 'tax_query' ] = array( array(
+ 'taxonomy' => $taxonomy,
+ 'terms' => array_merge( $term_array, $excluded_terms )
+ ) );
</ins><span class="cx"> }
</span><span class="cx">
</span><del>- $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
-
- $order = $start ? 'ASC' : 'DESC';
-
- return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
</del><ins>+ return get_posts( $query_args );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /*
</span><span class="lines">@@ -1342,14 +1363,15 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 3.7.0
</span><span class="cx"> *
</span><del>- * @param string $format Optional. Link anchor format.
- * @param string $link Optional. Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in same category.
- * @param string $excluded_categories Optional. Excluded categories IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>-function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
- return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true );
</del><ins>+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 );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1358,13 +1380,14 @@
</span><span class="cx"> * @since 1.5.0
</span><span class="cx"> * @uses get_previous_post_link()
</span><span class="cx"> *
</span><del>- * @param string $format Optional. Link anchor format.
- * @param string $link Optional. Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> */
</span><del>-function previous_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
- echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_categories );
</del><ins>+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 );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1373,14 +1396,15 @@
</span><span class="cx"> * @since 3.7.0
</span><span class="cx"> * @uses get_next_post_link()
</span><span class="cx"> *
</span><del>- * @param string $format Optional. Link anchor format.
- * @param string $link Optional. Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in same category.
- * @param string $excluded_categories Optional. Excluded categories IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>-function get_next_post_link( $format = '« %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
- return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false );
</del><ins>+function get_next_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, false, $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1388,13 +1412,14 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 1.5.0
</span><span class="cx"> *
</span><del>- * @param string $format Optional. Link anchor format.
- * @param string $link Optional. Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
</del><ins>+ * @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'.
</ins><span class="cx"> */
</span><del>-function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
- echo get_next_post_link( $format, $link, $in_same_cat, $excluded_categories );
</del><ins>+function next_post_link( $format = '%link »', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
+ echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1404,18 +1429,19 @@
</span><span class="cx"> *
</span><span class="cx"> * @since 3.7.0
</span><span class="cx"> *
</span><del>- * @param string $format Link anchor format.
- * @param string $link Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
</del><ins>+ * @param string $format Link anchor format.
+ * @param string $link 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 terms 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'.
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>-function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
</del><ins>+function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
</ins><span class="cx"> if ( $previous && is_attachment() )
</span><span class="cx"> $post = get_post( get_post()->post_parent );
</span><span class="cx"> else
</span><del>- $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
</del><ins>+ $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
</ins><span class="cx">
</span><span class="cx"> if ( ! $post ) {
</span><span class="cx"> $output = '';
</span><span class="lines">@@ -1451,15 +1477,16 @@
</span><span class="cx"> * @since 2.5.0
</span><span class="cx"> * @uses get_adjacent_post_link()
</span><span class="cx"> *
</span><del>- * @param string $format Link anchor format.
- * @param string $link Link permalink format.
- * @param bool $in_same_cat Optional. Whether link should be in a same category.
- * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
- * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
</del><ins>+ * @param string $format Link anchor format.
+ * @param string $link Link permalink format.
+ * @param bool $in_same_cat Optional. Whether link should be in a same category.
+ * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category 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'.
</ins><span class="cx"> * @return string
</span><span class="cx"> */
</span><del>-function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
- echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, $previous );
</del><ins>+function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
+ echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestslinkphp"></a>
<div class="modfile"><h4>Modified: trunk/tests/phpunit/tests/link.php (25958 => 25959)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/tests/phpunit/tests/link.php 2013-10-27 21:13:55 UTC (rev 25958)
+++ trunk/tests/phpunit/tests/link.php 2013-10-27 21:39:51 UTC (rev 25959)
</span><span class="lines">@@ -96,4 +96,75 @@
</span><span class="cx"> $wp_rewrite->flush_rules();
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ /**
+ * @ticket 17807
+ */
+ function test_get_adjacent_post() {
+ // Need some sample posts to test adjacency
+ $post_one = $this->factory->post->create_and_get( array(
+ 'post_title' => 'First',
+ 'post_date' => '2012-01-01 12:00:00'
+ ) );
+
+ $post_two = $this->factory->post->create_and_get( array(
+ 'post_title' => 'Second',
+ 'post_date' => '2012-02-01 12:00:00'
+ ) );
+
+ $post_three = $this->factory->post->create_and_get( array(
+ 'post_title' => 'Third',
+ 'post_date' => '2012-03-01 12:00:00'
+ ) );
+
+ $post_four = $this->factory->post->create_and_get( array(
+ 'post_title' => 'Fourth',
+ 'post_date' => '2012-04-01 12:00:00'
+ ) );
+
+ // Assign some terms
+ wp_set_object_terms( $post_one->ID, 'wordpress', 'category', false );
+ wp_set_object_terms( $post_three->ID, 'wordpress', 'category', false );
+
+ wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false );
+ wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false );
+
+ // Test normal post adjacency
+ $this->go_to( get_permalink( $post_two->ID ) );
+
+ $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) );
+ $this->assertEquals( $post_three, get_adjacent_post( false, '', false ) );
+
+ $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) );
+ $this->assertNotEquals( $post_two, get_adjacent_post( false, '', false ) );
+
+ // Test category adjacency
+ $this->go_to( get_permalink( $post_one->ID ) );
+
+ $this->assertEquals( '', get_adjacent_post( true, '', true, 'category' ) );
+ $this->assertEquals( $post_three, get_adjacent_post( true, '', false, 'category' ) );
+
+ // Test tag adjacency
+ $this->go_to( get_permalink( $post_two->ID ) );
+
+ $this->assertEquals( '', get_adjacent_post( true, '', true, 'post_tag' ) );
+ $this->assertEquals( $post_four, get_adjacent_post( true, '', false, 'post_tag' ) );
+
+ // Test normal boundary post
+ $this->go_to( get_permalink( $post_two->ID ) );
+
+ $this->assertEquals( array( $post_one ), get_boundary_post( false, '', true ) );
+ $this->assertEquals( array( $post_four ), get_boundary_post( false, '', false ) );
+
+ // Test category boundary post
+ $this->go_to( get_permalink( $post_one->ID ) );
+
+ $this->assertEquals( array( $post_one ), get_boundary_post( true, '', true, 'category' ) );
+ $this->assertEquals( array( $post_three ), get_boundary_post( true, '', false, 'category' ) );
+
+ // Test tag boundary post
+ $this->go_to( get_permalink( $post_two->ID ) );
+
+ $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
+ $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
+ }
</ins><span class="cx"> }
</span><span class="cx">\ No newline at end of file
</span></span></pre>
</div>
</div>
</body>
</html>