Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Dec 15th, 2010  |  syntax: PHP  |  size: 2.25 KB  |  hits: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function get_adjacent_post($in_same_category = false, $previous = true, $excluded_categories = '', $taxonomy = 'post') {
  2.         global $post, $wpdb;
  3.        
  4.         if ( empty( $post ) || !taxonomy_exists( $taxonomy ) )
  5.                 return null;
  6.  
  7.         $current_post_date = $post->post_date;
  8.  
  9.         $join = '';
  10.         $posts_in_ex_cats_sql = '';
  11.         if ( $in_same_category || !empty($excluded_categories) ) {
  12.                 $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";
  13.  
  14.                 if ( $in_same_category ) {
  15.                         $cat_array = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
  16.                         $join .= " AND tt.taxonomy = '".$taxonomy."' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
  17.                 }
  18.  
  19.                 $posts_in_ex_cats_sql = "AND tt.taxonomy = '".$taxonomy."'";
  20.                 if ( !empty($excluded_categories) ) {
  21.                         $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
  22.                         if ( !empty($cat_array) ) {
  23.                                 $excluded_categories = array_diff($excluded_categories, $cat_array);
  24.                                 $posts_in_ex_cats_sql = '';
  25.                         }
  26.  
  27.                         if ( !empty($excluded_categories) ) {
  28.                                 $posts_in_ex_cats_sql = " AND tt.taxonomy = '".$taxonomy."' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  29.                         }
  30.                 }
  31.         }
  32.  
  33.         $adjacent = $previous ? 'previous' : 'next';
  34.         $op = $previous ? '<' : '>';
  35.         $order = $previous ? 'DESC' : 'ASC';
  36.  
  37.         $join  = apply_filters( "get_{$adjacent}_{$taxonomy}_join", $join, $in_same_category, $excluded_categories );
  38.         $where = apply_filters( "get_{$adjacent}_{$taxonomy}_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_category, $excluded_categories );
  39.         $sort  = apply_filters( "get_{$adjacent}_{$taxonomy}_sort", "ORDER BY p.post_date $order LIMIT 1" );
  40.  
  41.         $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
  42.         $query_key = "adjacent_{$taxonomy}_" . md5($query);
  43.         $result = wp_cache_get($query_key, 'counts');
  44.         if ( false !== $result )
  45.                 return $result;
  46.  
  47.         $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
  48.         if ( null === $result )
  49.                 $result = '';
  50.  
  51.         wp_cache_set($query_key, $result, 'counts');
  52.         return $result;
  53. }