Make WordPress Core


Ignore:
Timestamp:
07/28/2011 05:19:51 PM (13 years ago)
Author:
ryan
Message:

Support an array or comma-seperated list of excluded category IDs in get_adjacent_post(). Props solarissmoke. fixes #17673

File:
1 edited

Legend:

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

    r18328 r18477  
    10661066 *
    10671067 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1068  * @param string $excluded_categories Optional. Excluded categories IDs.
     1068 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10691069 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10701070 */
     
    10791079 *
    10801080 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1081  * @param string $excluded_categories Optional. Excluded categories IDs.
     1081 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10821082 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10831083 */
     
    10941094 *
    10951095 * @param bool $in_same_cat Optional. Whether post should be in same category.
    1096  * @param string $excluded_categories Optional. Excluded categories IDs.
     1096 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    10971097 * @param bool $previous Optional. Whether to retrieve previous post.
    10981098 * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
    10991099 */
    1100 function get_adjacent_post($in_same_cat = false, $excluded_categories = '', $previous = true) {
     1100function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) {
    11011101    global $post, $wpdb;
    11021102
     
    11081108    $join = '';
    11091109    $posts_in_ex_cats_sql = '';
    1110     if ( $in_same_cat || !empty($excluded_categories) ) {
     1110    if ( $in_same_cat || ! empty( $excluded_categories ) ) {
    11111111        $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";
    11121112
     
    11171117
    11181118        $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'";
    1119         if ( !empty($excluded_categories) ) {
    1120             $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
    1121             if ( !empty($cat_array) ) {
     1119        if ( ! empty( $excluded_categories ) ) {
     1120            if ( ! is_array( $excluded_categories ) ) {
     1121                // back-compat, $excluded_categories used to be IDs separated by " and "
     1122                if ( strpos( $excluded_categories, ' and ' ) !== false )
     1123                    $excluded_categories = explode( ' and ', $excluded_categories );
     1124                else
     1125                    $excluded_categories = explode( ',', $excluded_categories );
     1126            }
     1127
     1128            $excluded_categories = array_map( 'intval', $excluded_categories );
     1129               
     1130            if ( ! empty( $cat_array ) ) {
    11221131                $excluded_categories = array_diff($excluded_categories, $cat_array);
    11231132                $posts_in_ex_cats_sql = '';
     
    11611170 * @param string $title Optional. Link title format.
    11621171 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1163  * @param string $excluded_categories Optional. Excluded categories IDs.
     1172 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    11641173 * @param bool $previous Optional, default is true. Whether display link to previous post.
    11651174 * @return string
     
    11981207 * @param string $title Optional. Link title format.
    11991208 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1200  * @param string $excluded_categories Optional. Excluded categories IDs.
     1209 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12011210 */
    12021211function adjacent_posts_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     
    12251234 * @param string $title Optional. Link title format.
    12261235 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1227  * @param string $excluded_categories Optional. Excluded categories IDs.
     1236 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12281237 */
    12291238function next_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     
    12381247 * @param string $title Optional. Link title format.
    12391248 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1240  * @param string $excluded_categories Optional. Excluded categories IDs.
     1249 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12411250 */
    12421251function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     
    12531262 *
    12541263 * @param bool $in_same_cat Optional. Whether returned post should be in same category.
    1255  * @param string $excluded_categories Optional. Excluded categories IDs.
     1264 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    12561265 * @param bool $start Optional. Whether to retrieve first or last post.
    12571266 * @return object
    12581267 */
    1259 function get_boundary_post($in_same_cat = false, $excluded_categories = '', $start = true) {
     1268function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
    12601269    global $post;
    12611270
    1262     if ( empty($post) || !is_single() || is_attachment() )
     1271    if ( empty($post) || ! is_single() || is_attachment() )
    12631272        return null;
    12641273
    12651274    $cat_array = array();
    1266     $excluded_categories = array();
    1267     if ( !empty($in_same_cat) || !empty($excluded_categories) ) {
    1268         if ( !empty($in_same_cat) ) {
    1269             $cat_array = wp_get_object_terms($post->ID, 'category', array('fields' => 'ids'));
    1270         }
    1271 
    1272         if ( !empty($excluded_categories) ) {
    1273             $excluded_categories = array_map('intval', explode(',', $excluded_categories));
    1274 
    1275             if ( !empty($cat_array) )
    1276                 $excluded_categories = array_diff($excluded_categories, $cat_array);
     1275    if( ! is_array( $excluded_categories ) )
     1276        $excluded_categories = explode( ',', $excluded_categories );
     1277       
     1278    if ( $in_same_cat || ! empty( $excluded_categories ) ) {
     1279        if ( $in_same_cat )
     1280            $cat_array = wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'ids' ) );
     1281
     1282        if ( ! empty( $excluded_categories ) ) {
     1283            $excluded_categories = array_map( 'intval', $excluded_categories );
     1284            $excluded_categories = array_diff( $excluded_categories, $cat_array );
    12771285
    12781286            $inverse_cats = array();
    1279             foreach ( $excluded_categories as $excluded_category)
     1287            foreach ( $excluded_categories as $excluded_category )
    12801288                $inverse_cats[] = $excluded_category * -1;
    12811289            $excluded_categories = $inverse_cats;
     
    12831291    }
    12841292
    1285     $categories = implode(',', array_merge($cat_array, $excluded_categories) );
     1293    $categories = implode( ',', array_merge( $cat_array, $excluded_categories ) );
    12861294
    12871295    $order = $start ? 'ASC' : 'DESC';
     
    12991307 * @param string $title Optional. Link title format.
    13001308 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1301  * @param string $excluded_categories Optional. Excluded categories IDs.
     1309 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13021310 * @param bool $start Optional, default is true. Whether display link to first or last post.
    13031311 * @return string
     
    13361344 * @param string $title Optional. Link title format.
    13371345 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1338  * @param string $excluded_categories Optional. Excluded categories IDs.
     1346 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    13391347 */
    13401348function start_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '') {
     
    14081416 * @param string $link Optional. Link permalink format.
    14091417 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1410  * @param string $excluded_categories Optional. Excluded categories IDs.
     1418 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14111419 */
    14121420function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '') {
     
    14221430 * @param string $link Optional. Link permalink format.
    14231431 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1424  * @param string $excluded_categories Optional. Excluded categories IDs.
     1432 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14251433 */
    14261434function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') {
     
    14381446 * @param string $link Link permalink format.
    14391447 * @param bool $in_same_cat Optional. Whether link should be in same category.
    1440  * @param string $excluded_categories Optional. Excluded categories IDs.
     1448 * @param array|string $excluded_categories Optional. Array or comma-separated list of excluded category IDs.
    14411449 * @param bool $previous Optional, default is true. Whether display link to previous post.
    14421450 */
Note: See TracChangeset for help on using the changeset viewer.