| 359 | | global $post, $wpdb; |
| 360 | | |
| 361 | | if( empty($post) || !is_single() || is_attachment() ) |
| 362 | | return null; |
| 363 | | |
| 364 | | $current_post_date = $post->post_date; |
| 365 | | |
| 366 | | $join = ''; |
| 367 | | if ( $in_same_cat ) { |
| 368 | | $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id "; |
| 369 | | $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids'); |
| 370 | | $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]); |
| 371 | | for ( $i = 1; $i < (count($cat_array)); $i++ ) { |
| 372 | | $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = %d', $cat_array[$i]); |
| 373 | | } |
| 374 | | $join .= ')'; |
| 375 | | } |
| 376 | | |
| 377 | | $sql_exclude_cats = ''; |
| 378 | | if ( !empty($excluded_categories) ) { |
| 379 | | $blah = explode(' and ', $excluded_categories); |
| 380 | | $posts_in_ex_cats = get_objects_in_term($blah, 'category'); |
| 381 | | $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; |
| 382 | | } |
| 383 | | |
| 384 | | $join = apply_filters( 'get_previous_post_join', $join, $in_same_cat, $excluded_categories ); |
| 385 | | $where = apply_filters( 'get_previous_post_where', $wpdb->prepare("WHERE p.post_date < %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); |
| 386 | | $sort = apply_filters( 'get_previous_post_sort', 'ORDER BY p.post_date DESC LIMIT 1' ); |
| 387 | | |
| 388 | | return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort"); |
| | 359 | return get_adjacent_post($in_same_cat, $excluded_categories); |
| 400 | | if ( $in_same_cat ) { |
| 401 | | $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id "; |
| 402 | | $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=tt_ids'); |
| 403 | | $join .= $wpdb->prepare(' AND (tr.term_taxonomy_id = %d', $cat_array[0]); |
| 404 | | for ( $i = 1; $i < (count($cat_array)); $i++ ) { |
| 405 | | $join .= $wpdb->prepare(' OR tr.term_taxonomy_id = $d', $cat_array[$i]); |
| | 375 | if ( $in_same_cat || !empty($excluded_categories) ) { |
| | 376 | $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"; |
| | 377 | |
| | 378 | if ( $in_same_cat ) { |
| | 379 | $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids'); |
| | 380 | $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($cat_array, ',') . ')'; |
| 410 | | $sql_exclude_cats = ''; |
| 411 | | if ( !empty($excluded_categories) ) { |
| 412 | | $blah = explode(' and ', $excluded_categories); |
| 413 | | $posts_in_ex_cats = get_objects_in_term($blah, 'category'); |
| 414 | | $posts_in_ex_cats_sql = 'AND p.ID NOT IN (' . implode($posts_in_ex_cats, ',') . ')'; |
| | 383 | $posts_in_ex_cats_sql = "AND tt.taxonomy = 'category'"; |
| | 384 | if ( !empty($excluded_categories) ) { |
| | 385 | $excluded_categories = array_map('intval', explode(' and ', $excluded_categories)); |
| | 386 | if ( !empty($cat_array) ) { |
| | 387 | $excluded_categories = array_diff($excluded_categories, $cat_array); |
| | 388 | $posts_in_ex_cats_sql = ''; |
| | 389 | } |
| | 390 | |
| | 391 | if ( !empty($excluded_categories) ) { |
| | 392 | $posts_in_ex_cats_sql = " AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; |
| | 393 | } |
| | 394 | } |
| 417 | | $join = apply_filters( 'get_next_post_join', $join, $in_same_cat, $excluded_categories ); |
| 418 | | $where = apply_filters( 'get_next_post_where', $wpdb->prepare("WHERE p.post_date > %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql AND p.ID != %d", $current_post_date, $post->ID), $in_same_cat, $excluded_categories ); |
| 419 | | $sort = apply_filters( 'get_next_post_sort', 'ORDER BY p.post_date ASC LIMIT 1' ); |
| | 397 | $adjacent = $previous ? 'previous' : 'next'; |
| | 398 | $op = $previous ? '<' : '>'; |
| | 399 | $order = $previous ? 'DESC' : 'ASC'; |
| 421 | | return @$wpdb->get_row("SELECT p.ID, p.post_title FROM $wpdb->posts AS p $join $where $sort"); |
| | 401 | $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories ); |
| | 402 | $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = 'post' AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date), $in_same_cat, $excluded_categories ); |
| | 403 | $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); |
| | 404 | |
| | 405 | return $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); |
| 427 | | if ( is_attachment() ) |
| | 412 | function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') { |
| | 413 | adjacent_post_link($format, $link, $in_same_cat, $excluded_categories, false); |
| | 414 | } |
| | 415 | |
| | 416 | function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { |
| | 417 | if ( $previous && is_attachment() ) |
| 450 | | function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '') { |
| 451 | | $post = get_next_post($in_same_cat, $excluded_categories); |
| 452 | | |
| 453 | | if ( !$post ) |
| 454 | | return; |
| 455 | | |
| 456 | | $title = $post->post_title; |
| 457 | | |
| 458 | | if ( empty($post->post_title) ) |
| 459 | | $title = __('Next Post'); |
| 460 | | |
| 461 | | $title = apply_filters('the_title', $title, $post); |
| 462 | | $string = '<a href="'.get_permalink($post->ID).'">'; |
| 463 | | $link = str_replace('%title', $title, $link); |
| 464 | | $link = $string . $link . '</a>'; |
| 465 | | $format = str_replace('%link', $link, $format); |
| 466 | | |
| 467 | | echo $format; |
| 468 | | } |
| 469 | | |