WordPress.org

Make WordPress Core

Changeset 25959


Ignore:
Timestamp:
10/27/2013 09:39:51 PM (5 years ago)
Author:
nacin
Message:

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 #17807.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r25868 r25959  
    10921092 * @since 1.5.0
    10931093 *
    1094  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1095  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1096  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    1097  */
    1098 function get_previous_post($in_same_cat = false, $excluded_categories = '') {
    1099     return get_adjacent_post($in_same_cat, $excluded_categories);
     1094 * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
     1095 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1096 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1097 * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     1098 */
     1099function get_previous_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1100    return get_adjacent_post( $in_same_term, $excluded_terms, true, $taxonomy );
    11001101}
    11011102
     
    11051106 * @since 1.5.0
    11061107 *
    1107  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1108  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1109  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    1110  */
    1111 function get_next_post($in_same_cat = false, $excluded_categories = '') {
    1112     return get_adjacent_post($in_same_cat, $excluded_categories, false);
     1108 * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
     1109 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1110 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1111 * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     1112 */
     1113function get_next_post( $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1114    return get_adjacent_post( $in_same_term, $excluded_terms, false, $taxonomy );
    11131115}
    11141116
     
    11201122 * @since 2.5.0
    11211123 *
    1122  * @param bool $in_same_cat Optional. Whether post should be in a same category.
    1123  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1124  * @param bool $previous Optional. Whether to retrieve previous post.
    1125  * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    1126  */
    1127 function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1124 * @param bool         $in_same_term   Optional. Whether post should be in a same taxonomy term.
     1125 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1126 * @param bool         $previous       Optional. Whether to retrieve previous post.
     1127 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1128 * @return mixed       Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
     1129 */
     1130function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    11281131    global $wpdb;
    11291132
    1130     if ( ! $post = get_post() )
     1133    if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
    11311134        return null;
    11321135
     
    11341137
    11351138    $join = '';
    1136     $posts_in_ex_cats_sql = '';
    1137     if ( $in_same_cat || ! empty( $excluded_categories ) ) {
     1139    $posts_in_ex_terms_sql = '';
     1140    if ( $in_same_term || ! empty( $excluded_terms ) ) {
    11381141        $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";
    11391142
    1140         if ( $in_same_cat ) {
    1141             if ( ! is_object_in_taxonomy( $post->post_type, 'category' ) )
     1143        if ( $in_same_term ) {
     1144            if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
    11421145                return '';
    1143             $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
    1144             if ( ! $cat_array || is_wp_error( $cat_array ) )
     1146            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1147            if ( ! $term_array || is_wp_error( $term_array ) )
    11451148                return '';
    1146             $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
     1149            $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
    11471150        }
    11481151
    1149         $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    1150         if ( ! empty( $excluded_categories ) ) {
    1151             if ( ! is_array( $excluded_categories ) ) {
    1152                 // back-compat, $excluded_categories used to be IDs separated by " and "
    1153                 if ( strpos( $excluded_categories, ' and ' ) !== false ) {
    1154                     _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded categories.' ), "'and'" ) );
    1155                     $excluded_categories = explode( ' and ', $excluded_categories );
     1152        $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
     1153        if ( ! empty( $excluded_terms ) ) {
     1154            if ( ! is_array( $excluded_terms ) ) {
     1155                // back-compat, $excluded_terms used to be $excluded_terms with IDs separated by " and "
     1156                if ( false !== strpos( $excluded_terms, ' and ' ) ) {
     1157                    _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
     1158                    $excluded_terms = explode( ' and ', $excluded_terms );
    11561159                } else {
    1157                     $excluded_categories = explode( ',', $excluded_categories );
     1160                    $excluded_terms = explode( ',', $excluded_terms );
    11581161                }
    11591162            }
    11601163
    1161             $excluded_categories = array_map( 'intval', $excluded_categories );
    1162 
    1163             if ( ! empty( $cat_array ) ) {
    1164                 $excluded_categories = array_diff($excluded_categories, $cat_array);
    1165                 $posts_in_ex_cats_sql = '';
     1164            $excluded_terms = array_map( 'intval', $excluded_terms );
     1165
     1166            if ( ! empty( $term_array ) ) {
     1167                $excluded_terms = array_diff( $excluded_terms, $term_array );
     1168                $posts_in_ex_terms_sql = '';
    11661169            }
    11671170
    1168             if ( !empty($excluded_categories) ) {
    1169                 $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
     1171            if ( ! empty( $excluded_terms ) ) {
     1172                $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
    11701173            }
    11711174        }
     
    11761179    $order = $previous ? 'DESC' : 'ASC';
    11771180
    1178     $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    1179     $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 );
     1181    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
     1182    $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 );
    11801183    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
    11811184
    11821185    $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
    1183     $query_key = 'adjacent_post_' . md5($query);
    1184     $result = wp_cache_get($query_key, 'counts');
     1186    $query_key = 'adjacent_post_' . md5( $query );
     1187    $result = wp_cache_get( $query_key, 'counts' );
    11851188    if ( false !== $result ) {
    11861189        if ( $result )
     
    11931196        $result = '';
    11941197
    1195     wp_cache_set($query_key, $result, 'counts');
     1198    wp_cache_set( $query_key, $result, 'counts' );
    11961199
    11971200    if ( $result )
     
    12081211 * @since 2.8.0
    12091212 *
    1210  * @param string $title Optional. Link title format.
    1211  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1212  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1213  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
    1214  * @return string
    1215  */
    1216 function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
     1213 * @param string       $title          Optional. Link title format.
     1214 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1215 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1216 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1217 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1218 * @return string
     1219 */
     1220function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    12171221    if ( $previous && is_attachment() && $post = get_post() )
    12181222        $post = get_post( $post->post_parent );
    12191223    else
    1220         $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
    1221 
    1222     if ( empty($post) )
     1224        $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
     1225
     1226    if ( empty( $post ) )
    12231227        return;
    12241228
     
    12261230
    12271231    if ( empty( $post_title ) )
    1228         $post_title = $previous ? __('Previous Post') : __('Next Post');
    1229 
    1230     $date = mysql2date(get_option('date_format'), $post->post_date);
    1231 
    1232     $title = str_replace('%title', $post_title, $title);
    1233     $title = str_replace('%date', $date, $title);
     1232        $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
     1233
     1234    $date = mysql2date( get_option( 'date_format' ), $post->post_date );
     1235
     1236    $title = str_replace( '%title', $post_title, $title );
     1237    $title = str_replace( '%date', $date, $title );
    12341238
    12351239    $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
    12361240    $link .= esc_attr( $title );
    1237     $link .= "' href='" . get_permalink($post) . "' />\n";
     1241    $link .= "' href='" . get_permalink( $post ) . "' />\n";
    12381242
    12391243    $adjacent = $previous ? 'previous' : 'next';
     
    12461250 * @since 2.8.0
    12471251 *
    1248  * @param string $title Optional. Link title format.
    1249  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1250  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1251  */
    1252 function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1253     echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
    1254     echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1252 * @param string       $title          Optional. Link title format.
     1253 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1254 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1255 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1256 */
     1257function adjacent_posts_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1258    echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
     1259    echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12551260}
    12561261
     
    12731278 * @since 2.8.0
    12741279 *
    1275  * @param string $title Optional. Link title format.
    1276  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1277  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1278  */
    1279 function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1280     echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', false);
     1280 * @param string       $title          Optional. Link title format.
     1281 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1282 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1283 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1284 */
     1285function next_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1286    echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', false, $taxonomy );
    12811287}
    12821288
     
    12861292 * @since 2.8.0
    12871293 *
    1288  * @param string $title Optional. Link title format.
    1289  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1290  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1291  */
    1292 function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
    1293     echo get_adjacent_post_rel_link($title, $in_same_cat, $excluded_categories = '', true);
     1294 * @param string       $title          Optional. Link title format.
     1295 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1296 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs. Default true.
     1297 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1298 */
     1299function prev_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1300    echo get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms = '', true, $taxonomy );
    12941301}
    12951302
     
    12981305 *
    12991306 * Boundary being either the first or last post by publish date within the constraints specified
    1300  * by $in_same_cat or $excluded_categories.
     1307 * by $in_same_term or $excluded_terms.
    13011308 *
    13021309 * @since 2.8.0
    13031310 *
    1304  * @param bool $in_same_cat Optional. Whether returned post should be in a same category.
    1305  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1306  * @param bool $start Optional. Whether to retrieve first or last post.
     1311 * @param bool         $in_same_term   Optional. Whether returned post should be in a same taxonomy term.
     1312 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1313 * @param bool         $start          Optional. Whether to retrieve first or last post.
     1314 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
    13071315 * @return object
    13081316 */
    1309 function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
     1317function get_boundary_post( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' ) {
    13101318    $post = get_post();
    1311     if ( ! $post || ! is_single() || is_attachment() )
     1319    if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
    13121320        return null;
    13131321
    1314     $cat_array = array();
    1315     if( ! is_array( $excluded_categories ) )
    1316         $excluded_categories = explode( ',', $excluded_categories );
    1317 
    1318     if ( $in_same_cat || ! empty( $excluded_categories ) ) {
    1319         if ( $in_same_cat )
    1320             $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
    1321 
    1322         if ( ! empty( $excluded_categories ) ) {
    1323             $excluded_categories = array_map( 'intval', $excluded_categories );
    1324             $excluded_categories = array_diff( $excluded_categories, $cat_array );
    1325 
    1326             $inverse_cats = array();
    1327             foreach ( $excluded_categories as $excluded_category )
    1328                 $inverse_cats[] = $excluded_category * -1;
    1329             $excluded_categories = $inverse_cats;
     1322    $query_args = array(
     1323        'posts_per_page' => 1,
     1324        'order' => $start ? 'ASC' : 'DESC',
     1325        'update_post_term_cache' => false,
     1326        'update_post_meta_cache' => false
     1327    );
     1328
     1329    $term_array = array();
     1330
     1331    if ( ! is_array( $excluded_terms ) ) {
     1332        if ( ! empty( $excluded_terms ) )
     1333            $excluded_terms = explode( ',', $excluded_terms );
     1334        else
     1335            $excluded_terms = array();
     1336    }
     1337
     1338    if ( $in_same_term || ! empty( $excluded_terms ) ) {
     1339        if ( $in_same_term )
     1340            $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
     1341
     1342        if ( ! empty( $excluded_terms ) ) {
     1343            $excluded_terms = array_map( 'intval', $excluded_terms );
     1344            $excluded_terms = array_diff( $excluded_terms, $term_array );
     1345
     1346            $inverse_terms = array();
     1347            foreach ( $excluded_terms as $excluded_term )
     1348                $inverse_terms[] = $excluded_term * -1;
     1349            $excluded_terms = $inverse_terms;
    13301350        }
    1331     }
    1332 
    1333     $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
    1334 
    1335     $order = $start ? 'ASC' : 'DESC';
    1336 
    1337     return get_posts( array('numberposts' => 1, 'category' => $categories, 'order' => $order, 'update_post_term_cache' => false, 'update_post_meta_cache' => false) );
     1351
     1352        $query_args[ 'tax_query' ] = array( array(
     1353            'taxonomy' => $taxonomy,
     1354            'terms' => array_merge( $term_array, $excluded_terms )
     1355        ) );
     1356    }
     1357
     1358    return get_posts( $query_args );
    13381359}
    13391360
     
    13431364 * @since 3.7.0
    13441365 *
    1345  * @param string $format Optional. Link anchor format.
    1346  * @param string $link Optional. Link permalink format.
    1347  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1348  * @param string $excluded_categories Optional. Excluded categories IDs.
    1349  * @return string
    1350  */
    1351 function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1352     return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, true );
     1366 * @param string       $format         Optional. Link anchor format.
     1367 * @param string       $link           Optional. Link permalink format.
     1368 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1369 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1370 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1371 * @return string
     1372 */
     1373function get_previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1374    return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, true, $taxonomy );
    13531375}
    13541376
     
    13591381 * @uses get_previous_post_link()
    13601382 *
    1361  * @param string $format Optional. Link anchor format.
    1362  * @param string $link Optional. Link permalink format.
    1363  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1364  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1365  */
    1366 function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1367     echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1383 * @param string       $format         Optional. Link anchor format.
     1384 * @param string       $link           Optional. Link permalink format.
     1385 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1386 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1387 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1388 */
     1389function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1390    echo get_previous_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
    13681391}
    13691392
     
    13741397 * @uses get_next_post_link()
    13751398 *
    1376  * @param string $format Optional. Link anchor format.
    1377  * @param string $link Optional. Link permalink format.
    1378  * @param bool $in_same_cat Optional. Whether link should be in same category.
    1379  * @param string $excluded_categories Optional. Excluded categories IDs.
    1380  * @return string
    1381  */
    1382 function get_next_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1383     return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, false );
     1399 * @param string       $format         Optional. Link anchor format.
     1400 * @param string       $link           Optional. Link permalink format.
     1401 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1402 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1403 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1404 * @return string
     1405 */
     1406function get_next_post_link( $format = '&laquo; %link', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1407    return get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, false, $taxonomy );
    13841408}
    13851409
     
    13891413 * @since 1.5.0
    13901414 *
    1391  * @param string $format Optional. Link anchor format.
    1392  * @param string $link Optional. Link permalink format.
    1393  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1394  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1395  */
    1396 function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_categories = '' ) {
    1397      echo get_next_post_link( $format, $link, $in_same_cat, $excluded_categories );
     1415 * @param string       $format         Optional. Link anchor format.
     1416 * @param string       $link           Optional. Link permalink format.
     1417 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1418 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
     1419 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1420 */
     1421function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ) {
     1422     echo get_next_post_link( $format, $link, $in_same_cat, $excluded_terms, $taxonomy );
    13981423}
    13991424
     
    14051430 * @since 3.7.0
    14061431 *
    1407  * @param string $format Link anchor format.
    1408  * @param string $link Link permalink format.
    1409  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1410  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1411  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
    1412  * @return string
    1413  */
    1414 function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
     1432 * @param string       $format         Link anchor format.
     1433 * @param string       $link           Link permalink format.
     1434 * @param bool         $in_same_term   Optional. Whether link should be in a same taxonomy term.
     1435 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded terms IDs.
     1436 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1437 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1438 * @return string
     1439 */
     1440function get_adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
    14151441    if ( $previous && is_attachment() )
    14161442        $post = get_post( get_post()->post_parent );
    14171443    else
    1418         $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
     1444        $post = get_adjacent_post( $in_same_cat, $excluded_terms, $previous, $taxonomy );
    14191445
    14201446    if ( ! $post ) {
     
    14521478 * @uses get_adjacent_post_link()
    14531479 *
    1454  * @param string $format Link anchor format.
    1455  * @param string $link Link permalink format.
    1456  * @param bool $in_same_cat Optional. Whether link should be in a same category.
    1457  * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    1458  * @param bool $previous Optional, default is true. Whether to display link to previous or next post.
    1459  * @return string
    1460  */
    1461 function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true ) {
    1462     echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_categories, $previous );
     1480 * @param string       $format         Link anchor format.
     1481 * @param string       $link           Link permalink format.
     1482 * @param bool         $in_same_cat    Optional. Whether link should be in a same category.
     1483 * @param array|string $excluded_terms Optional. Array or comma-separated list of excluded category IDs.
     1484 * @param bool         $previous       Optional. Whether to display link to previous or next post. Default true.
     1485 * @param string       $taxonomy       Optional. Taxonomy, if $in_same_term is true. Default 'category'.
     1486 * @return string
     1487 */
     1488function adjacent_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
     1489    echo get_adjacent_post_link( $format, $link, $in_same_cat, $excluded_terms, $previous, $taxonomy );
    14631490}
    14641491
  • trunk/tests/phpunit/tests/link.php

    r25404 r25959  
    9797    }
    9898
     99    /**
     100     * @ticket 17807
     101     */
     102    function test_get_adjacent_post() {
     103        // Need some sample posts to test adjacency
     104        $post_one = $this->factory->post->create_and_get( array(
     105            'post_title' => 'First',
     106            'post_date' => '2012-01-01 12:00:00'
     107        ) );
     108
     109        $post_two = $this->factory->post->create_and_get( array(
     110            'post_title' => 'Second',
     111            'post_date' => '2012-02-01 12:00:00'
     112        ) );
     113
     114        $post_three = $this->factory->post->create_and_get( array(
     115            'post_title' => 'Third',
     116            'post_date' => '2012-03-01 12:00:00'
     117        ) );
     118
     119        $post_four = $this->factory->post->create_and_get( array(
     120            'post_title' => 'Fourth',
     121            'post_date' => '2012-04-01 12:00:00'
     122        ) );
     123
     124        // Assign some terms
     125        wp_set_object_terms( $post_one->ID, 'wordpress', 'category', false );
     126        wp_set_object_terms( $post_three->ID, 'wordpress', 'category', false );
     127
     128        wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false );
     129        wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false );
     130
     131        // Test normal post adjacency
     132        $this->go_to( get_permalink( $post_two->ID ) );
     133
     134        $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) );
     135        $this->assertEquals( $post_three, get_adjacent_post( false, '', false ) );
     136
     137        $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) );
     138        $this->assertNotEquals( $post_two, get_adjacent_post( false, '', false ) );
     139
     140        // Test category adjacency
     141        $this->go_to( get_permalink( $post_one->ID ) );
     142
     143        $this->assertEquals( '', get_adjacent_post( true, '', true, 'category' ) );
     144        $this->assertEquals( $post_three, get_adjacent_post( true, '', false, 'category' ) );
     145
     146        // Test tag adjacency
     147        $this->go_to( get_permalink( $post_two->ID ) );
     148
     149        $this->assertEquals( '', get_adjacent_post( true, '', true, 'post_tag' ) );
     150        $this->assertEquals( $post_four, get_adjacent_post( true, '', false, 'post_tag' ) );
     151
     152        // Test normal boundary post
     153        $this->go_to( get_permalink( $post_two->ID ) );
     154
     155        $this->assertEquals( array( $post_one ), get_boundary_post( false, '', true ) );
     156        $this->assertEquals( array( $post_four ), get_boundary_post( false, '', false ) );
     157
     158        // Test category boundary post
     159        $this->go_to( get_permalink( $post_one->ID ) );
     160
     161        $this->assertEquals( array( $post_one ), get_boundary_post( true, '', true, 'category' ) );
     162        $this->assertEquals( array( $post_three ), get_boundary_post( true, '', false, 'category' ) );
     163
     164        // Test tag boundary post
     165        $this->go_to( get_permalink( $post_two->ID ) );
     166
     167        $this->assertEquals( array( $post_two ), get_boundary_post( true, '', true, 'post_tag' ) );
     168        $this->assertEquals( array( $post_four ), get_boundary_post( true, '', false, 'post_tag' ) );
     169    }
    99170}
Note: See TracChangeset for help on using the changeset viewer.