Guest User

Prev/Next Post Filtering

a guest
Dec 14th, 2011
22
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_filter('get_previous_post_join','sql_get_adjacent_post_join');
  2. add_filter('get_next_post_join','sql_get_adjacent_post_join');
  3. add_filter('get_previous_post_sort','sql_get_adjacent_post_sort');
  4. add_filter('get_next_post_sort','sql_get_adjacent_post_sort');
  5. add_filter( 'get_previous_post_where', 'filter_private_future_nextprevious_links' );
  6. add_filter( 'get_next_post_where', 'filter_private_future_nextprevious_links' );
  7.  
  8. function sql_get_adjacent_post_join($join) {
  9.     global $post, $wpdb;
  10.     if('story-images' == get_post_type() || 'editorials' == get_post_type()) {
  11.         return $join . " INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id";
  12.     }
  13. }
  14.  
  15.  
  16. function filter_private_future_nextprevious_links($where) {
  17.     global $post;
  18.    
  19.     //check for selected editorial and get ID to pass into $where
  20.     if('story-images' == get_post_type()) {
  21.         if(get_field('editorial')) {
  22.             foreach(get_field('editorial') as $post_object):
  23.                  $editorial_id = $post_object->ID;
  24.             endforeach;
  25.         }
  26.         return $where . " AND m.meta_key = 'editorial' AND m.meta_value = $editorial_id";
  27.     }
  28.    
  29.     //check for selected editorial and get ID to pass into $where
  30.     if('editorials' == get_post_type()) {
  31.        
  32.         if(get_field('author') == 'Randy') {
  33.             $this_id = $post->ID;
  34.             $stories = get_field('stories', 863);
  35.             $story_ids = array();
  36.             foreach($stories as $story) {
  37.                 $story_ids[] = $story->ID;
  38.             }
  39.             $ids_for_where = implode(',', $story_ids);
  40.             return $where . " AND m.meta_key = 'author' AND m.meta_value = 'Randy' AND p.ID IN ($ids_for_where)";
  41.         } elseif(get_field('author') == 'Melissa') {
  42.             $this_id = $post->ID;
  43.             $stories = get_field('stories', 881);
  44.             $story_ids = array();
  45.             foreach($stories as $story) {
  46.                 $story_ids[] = $story->ID;
  47.             }
  48.             $ids_for_where = implode(',', $story_ids);
  49.             return $where . " AND m.meta_key = 'author' AND m.meta_value = 'Melissa' AND p.ID IN ($ids_for_where)";
  50.         }  
  51.     }    
  52. }
  53.  
  54. function sql_get_adjacent_post_sort($sort) {
  55.     global $post, $wpdb;
  56.     $this_id = $post->ID;
  57.     if('story-images' == get_post_type()) {
  58.         if(($editorials = get_field('editorial'))) {
  59.             $editorial = current($editorials);
  60.             $editorial_id = $editorial->ID;
  61.            
  62.             $editorial_images = get_field('editorial-images', $editorial_id);
  63.             $editorial_image_ids = array();
  64.             foreach($editorial_images as $editorial_image) {
  65.                 $editorial_image_ids[] = $editorial_image->ID;
  66.             }
  67.            
  68.             if(!empty($editorial_image_ids)) {
  69.                 $ids_for_sort = implode(',', $editorial_image_ids);
  70.                 echo $ids_for_sort;
  71.                 $sort = "ORDER BY FIELD(p.ID, {$ids_for_sort})";
  72.             }
  73.         }
  74.     }
  75.     echo $sort;
  76.     return $sort;
  77. }
RAW Paste Data