This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Ambrosite Next/Previous Post Link Plus w/ same_parent

By: switzerbaden on Nov 8th, 2012  |  syntax: PHP  |  size: 17.06 KB  |  views: 106  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Plugin Name: Ambrosite Next/Previous Post Link Plus
  4. Plugin URI: http://www.ambrosite.com/plugins
  5. Description: Upgrades the next/previous post link template tags to reorder or loop adjacent post navigation links, return multiple links, truncate link titles, and display post thumbnails. IMPORTANT: If you are upgrading from plugin version 1.1, you will need to update your templates (refer to the <a href="http://www.ambrosite.com/plugins/next-previous-post-link-plus-for-wordpress">documentation</a> on configuring parameters).
  6. Version: 2.4
  7. Author: J. Michael Ambrosio
  8. Author URI: http://www.ambrosite.com
  9. License: GPL2
  10. */
  11.  
  12. /**
  13.  * Retrieve adjacent post link.
  14.  *
  15.  * Can either be next or previous post link.
  16.  *
  17.  * Based on get_adjacent_post() from wp-includes/link-template.php
  18.  *
  19.  * @param array $r Arguments.
  20.  * @param bool $previous Optional. Whether to retrieve previous post.
  21.  * @return array of post objects.
  22.  */
  23. function get_adjacent_post_plus($r, $previous = true ) {
  24.         global $post, $wpdb;
  25.  
  26.         extract( $r, EXTR_SKIP );
  27.  
  28.         if ( empty( $post ) )
  29.                 return null;
  30.  
  31. //      Sanitize $order_by, since we are going to use it in the SQL query. Default to 'post_date'.
  32.         if ( in_array($order_by, array('post_date', 'post_title', 'post_excerpt', 'post_name', 'post_modified')) ) {
  33.                 $order_format = '%s';
  34.         } elseif ( in_array($order_by, array('ID', 'post_author', 'post_parent', 'menu_order', 'comment_count')) ) {
  35.                 $order_format = '%d';
  36.         } elseif ( $order_by == 'custom' && !empty($meta_key) ) { // Don't allow a custom sort if meta_key is empty.
  37.                 $order_format = '%s';
  38.         } elseif ( $order_by == 'numeric' && !empty($meta_key) ) {
  39.                 $order_format = '%d';
  40.         } else {
  41.                 $order_by = 'post_date';
  42.                 $order_format = '%s';
  43.         }
  44.        
  45. //      Sanitize $order_2nd. Only columns containing unique values are allowed here. Default to 'post_date'.
  46.         if ( in_array($order_2nd, array('post_date', 'post_title', 'post_modified')) ) {
  47.                 $order_format2 = '%s';
  48.         } elseif ( in_array($order_2nd, array('ID')) ) {
  49.                 $order_format2 = '%d';
  50.         } else {
  51.                 $order_2nd = 'post_date';
  52.                 $order_format2 = '%s';
  53.         }
  54.        
  55. //      Sanitize num_results (non-integer or negative values trigger SQL errors)
  56.         $num_results = intval($num_results) < 2 ? 1 : intval($num_results);
  57.  
  58. //      Queries involving custom fields require an extra table join
  59.         if ( $order_by == 'custom' || $order_by == 'numeric' ) {
  60.                 $current_post = get_post_meta($post->ID, $meta_key, TRUE);
  61.                 $order_by = ($order_by === 'numeric') ? 'm.meta_value+0' : 'm.meta_value';
  62.                 $meta_join = $wpdb->prepare(" INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id AND m.meta_key = %s", $meta_key );
  63.         } elseif ( $in_same_meta ) {
  64.                 $current_post = $post->$order_by;
  65.                 $order_by = 'p.' . $order_by;
  66.                 $meta_join = $wpdb->prepare(" INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id AND m.meta_key = %s", $in_same_meta );
  67.         } else {
  68.                 $current_post = $post->$order_by;
  69.                 $order_by = 'p.' . $order_by;
  70.                 $meta_join = '';
  71.         }
  72.  
  73. //      Get the current post value for the second sort column
  74.         $current_post2 = $post->$order_2nd;
  75.         $order_2nd = 'p.' . $order_2nd;
  76.        
  77. //      Get the list of post types. Default to current post type
  78.         if ( empty($post_type) )
  79.                 $post_type = "'$post->post_type'";
  80.  
  81. //      Put this section in a do-while loop to enable the loop-to-first-post option
  82.         do {
  83.                 $join = $meta_join;
  84.                 $excluded_categories = $ex_cats;
  85.                 $included_categories = $in_cats;
  86.                 $excluded_posts = $ex_posts;
  87.                 $included_posts = $in_posts;
  88.                 $in_same_term_sql = $in_same_author_sql = $parent_sql = $in_same_meta_sql = $ex_cats_sql = $in_cats_sql = $ex_posts_sql = $in_posts_sql = '';
  89.  
  90. //              Get the list of hierarchical taxonomies, including customs (don't assume taxonomy = 'category')
  91.                 $taxonomies = array_filter( get_post_taxonomies($post->ID), "is_taxonomy_hierarchical" );
  92.  
  93.                 if ( ($in_same_cat || $in_same_tax || $in_same_format || !empty($excluded_categories) || !empty($included_categories)) && !empty($taxonomies) ) {
  94.                         $cat_array = $tax_array = $format_array = array();
  95.  
  96.                         if ( $in_same_cat ) {
  97.                                 $cat_array = wp_get_object_terms($post->ID, $taxonomies, array('fields' => 'ids'));
  98.                         }
  99.                         if ( $in_same_tax && !$in_same_cat ) {
  100.                                 if ( $in_same_tax === true ) {
  101.                                         if ( $taxonomies != array('category') )
  102.                                                 $taxonomies = array_diff($taxonomies, array('category'));
  103.                                 } else
  104.                                         $taxonomies = (array) $in_same_tax;
  105.                                 $tax_array = wp_get_object_terms($post->ID, $taxonomies, array('fields' => 'ids'));
  106.                         }
  107.                         if ( $in_same_format ) {
  108.                                 $taxonomies[] = 'post_format';
  109.                                 $format_array = wp_get_object_terms($post->ID, 'post_format', array('fields' => 'ids'));
  110.                         }
  111.  
  112.                         $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 AND tt.taxonomy IN (\"" . implode('", "', $taxonomies) . "\")";
  113.  
  114.                         $term_array = array_unique( array_merge( $cat_array, $tax_array, $format_array ) );
  115.                         if ( !empty($term_array) )
  116.                                 $in_same_term_sql = "AND tt.term_id IN (" . implode(',', $term_array) . ")";
  117.  
  118.                         if ( !empty($excluded_categories) ) {
  119. //                              Support for both (1 and 5 and 15) and (1, 5, 15) delimiter styles
  120.                                 $delimiter = ( strpos($excluded_categories, ',') !== false ) ? ',' : 'and';
  121.                                 $excluded_categories = array_map( 'intval', explode($delimiter, $excluded_categories) );
  122. //                              Three category exclusion methods are supported: 'strong', 'diff', and 'weak'.
  123. //                              Default is 'weak'. See the plugin documentation for more information.
  124.                                 if ( $ex_cats_method === 'strong' ) {
  125.                                         $taxonomies = array_filter( get_post_taxonomies($post->ID), "is_taxonomy_hierarchical" );
  126.                                         if ( function_exists('get_post_format') )
  127.                                                 $taxonomies[] = 'post_format';
  128.                                         $ex_cats_posts = get_objects_in_term( $excluded_categories, $taxonomies );
  129.                                         if ( !empty($ex_cats_posts) )
  130.                                                 $ex_cats_sql = "AND p.ID NOT IN (" . implode($ex_cats_posts, ',') . ")";
  131.                                 } else {
  132.                                         if ( !empty($term_array) && !in_array($ex_cats_method, array('diff', 'differential')) )
  133.                                                 $excluded_categories = array_diff($excluded_categories, $term_array);
  134.                                         if ( !empty($excluded_categories) )
  135.                                                 $ex_cats_sql = "AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';
  136.                                 }
  137.                         }
  138.  
  139.                         if ( !empty($included_categories) ) {
  140.                                 $in_same_term_sql = ''; // in_cats overrides in_same_cat
  141.                                 $delimiter = ( strpos($included_categories, ',') !== false ) ? ',' : 'and';
  142.                                 $included_categories = array_map( 'intval', explode($delimiter, $included_categories) );
  143.                                 $in_cats_sql = "AND tt.term_id IN (" . implode(',', $included_categories) . ")";
  144.                         }
  145.                 }
  146.  
  147. //              Optionally restrict next/previous links to same author         
  148.                 if ( $in_same_author )
  149.                         $in_same_author_sql = $wpdb->prepare("AND p.post_author = %d", $post->post_author );
  150.                        
  151. //              Optionally restrict parent of next/previous links      
  152.                 if ( $same_parent )
  153.                         $parent_sql = $wpdb->prepare("AND p.post_parent = %d", $post->post_parent );
  154.                        
  155. //              Optionally restrict next/previous links to same meta value
  156.                 if ( $in_same_meta && $r['order_by'] != 'custom' && $r['order_by'] != 'numeric' )
  157.                         $in_same_meta_sql = $wpdb->prepare("AND m.meta_value = %s", get_post_meta($post->ID, $in_same_meta, TRUE) );
  158.                        
  159. //              Optionally exclude individual post IDs
  160.                 if ( !empty($excluded_posts) ) {
  161.                         $excluded_posts = array_map( 'intval', explode(',', $excluded_posts) );
  162.                         $ex_posts_sql = " AND p.ID NOT IN (" . implode(',', $excluded_posts) . ")";
  163.                 }
  164.                
  165. //              Optionally include individual post IDs
  166.                 if ( !empty($included_posts) ) {
  167.                         $included_posts = array_map( 'intval', explode(',', $included_posts) );
  168.                         $in_posts_sql = " AND p.ID IN (" . implode(',', $included_posts) . ")";
  169.                 }
  170.  
  171.                 $adjacent = $previous ? 'previous' : 'next';
  172.                 $order = $previous ? 'DESC' : 'ASC';
  173.                 $op = $previous ? '<' : '>';
  174.  
  175. //              Optionally get the first/last post. Disable looping and return only one result.
  176.                 if ( $end_post ) {
  177.                         $order = $previous ? 'ASC' : 'DESC';
  178.                         $num_results = 1;
  179.                         $loop = false;
  180.                         if ( $end_post === 'fixed' ) // display the end post link even when it is the current post
  181.                                 $op = $previous ? '<=' : '>=';
  182.                 }
  183.  
  184. //              If there is no next/previous post, loop back around to the first/last post.            
  185.                 if ( $loop && isset($result) ) {
  186.                         $op = $previous ? '>=' : '<=';
  187.                         $loop = false; // prevent an infinite loop if no first/last post is found
  188.                 }
  189.                
  190.                 $join  = apply_filters( "get_{$adjacent}_post_plus_join", $join, $r );
  191.  
  192. //              In case the value in the $order_by column is not unique, select posts based on the $order_2nd column as well.
  193. //              This prevents posts from being skipped when they have, for example, the same menu_order.
  194.                 $where = apply_filters( "get_{$adjacent}_post_plus_where", $wpdb->prepare("WHERE ( $order_by $op $order_format OR $order_2nd $op $order_format2 AND $order_by = $order_format ) AND p.post_type IN ($post_type) AND p.post_status = 'publish' $in_same_term_sql $in_same_author_sql $parent_sql $in_same_meta_sql $ex_cats_sql $in_cats_sql $ex_posts_sql $in_posts_sql", $current_post, $current_post2, $current_post), $r );
  195.  
  196.                 $sort  = apply_filters( "get_{$adjacent}_post_plus_sort", "ORDER BY $order_by $order, $order_2nd $order LIMIT $num_results", $r );
  197.  
  198.                 $query = "SELECT DISTINCT p.* FROM $wpdb->posts AS p $join $where $sort";
  199.                 $query_key = 'adjacent_post_' . md5($query);
  200.                 $result = wp_cache_get($query_key);
  201.                 if ( false !== $result )
  202.                         return $result;
  203.  
  204. //              echo $query . '<br />';
  205.  
  206. //              Use get_results instead of get_row, in order to retrieve multiple adjacent posts (when $num_results > 1)
  207. //              Add DISTINCT keyword to prevent posts in multiple categories from appearing more than once
  208.                 $result = $wpdb->get_results("SELECT DISTINCT p.* FROM $wpdb->posts AS p $join $where $sort");
  209.                 if ( null === $result )
  210.                         $result = '';
  211.  
  212.         } while ( !$result && $loop );
  213.  
  214.         wp_cache_set($query_key, $result);
  215.         return $result;
  216. }
  217.  
  218. /**
  219.  * Display previous post link that is adjacent to the current post.
  220.  *
  221.  * Based on previous_post_link() from wp-includes/link-template.php
  222.  *
  223.  * @param array|string $args Optional. Override default arguments.
  224.  * @return bool True if previous post link is found, otherwise false.
  225.  */
  226. function previous_post_link_plus($args = '') {
  227.         return adjacent_post_link_plus($args, '&laquo; %link', true);
  228. }
  229.  
  230. /**
  231.  * Display next post link that is adjacent to the current post.
  232.  *
  233.  * Based on next_post_link() from wp-includes/link-template.php
  234.  *
  235.  * @param array|string $args Optional. Override default arguments.
  236.  * @return bool True if next post link is found, otherwise false.
  237.  */
  238. function next_post_link_plus($args = '') {
  239.         return adjacent_post_link_plus($args, '%link &raquo;', false);
  240. }
  241.  
  242. /**
  243.  * Display adjacent post link.
  244.  *
  245.  * Can be either next post link or previous.
  246.  *
  247.  * Based on adjacent_post_link() from wp-includes/link-template.php
  248.  *
  249.  * @param array|string $args Optional. Override default arguments.
  250.  * @param bool $previous Optional, default is true. Whether display link to previous post.
  251.  * @return bool True if next/previous post is found, otherwise false.
  252.  */
  253. function adjacent_post_link_plus($args = '', $format = '%link &raquo;', $previous = true) {
  254.         $defaults = array(
  255.                 'order_by' => 'post_date', 'order_2nd' => 'post_date', 'meta_key' => '', 'post_type' => '',
  256.                 'loop' => false, 'end_post' => false, 'thumb' => false, 'max_length' => 0,
  257.                 'format' => '', 'link' => '%title', 'date_format' => '', 'tooltip' => '%title',
  258.                 'in_same_cat' => false, 'in_same_tax' => false, 'in_same_format' => false,
  259.                 'in_same_author' => false, 'same_parent' => false, 'in_same_meta' => false,
  260.                 'ex_cats' => '', 'ex_cats_method' => 'weak', 'in_cats' => '', 'ex_posts' => '', 'in_posts' => '',
  261.                 'before' => '', 'after' => '', 'num_results' => 1, 'return' => false, 'echo' => true
  262.         );
  263.  
  264. //      If Post Types Order plugin is installed, default to sorting on menu_order
  265.         if ( function_exists('CPTOrderPosts') )
  266.                 $defaults['order_by'] = 'menu_order';
  267.        
  268.         $r = wp_parse_args( $args, $defaults );
  269.         if ( empty($r['format']) )
  270.                 $r['format'] = $format;
  271.         if ( empty($r['date_format']) )
  272.                 $r['date_format'] = get_option('date_format');
  273.         if ( !function_exists('get_post_format') )
  274.                 $r['in_same_format'] = false;
  275.  
  276.         if ( $previous && is_attachment() ) {
  277.                 $posts = array();
  278.                 $posts[] = & get_post($GLOBALS['post']->post_parent);
  279.         } else
  280.                 $posts = get_adjacent_post_plus($r, $previous);
  281.  
  282. //      If there is no next/previous post, return false so themes may conditionally display inactive link text.
  283.         if ( !$posts )
  284.                 return false;
  285.  
  286. //      If sorting by date, display posts in reverse chronological order. Otherwise display in alpha/numeric order.
  287.         if ( ($previous && $r['order_by'] != 'post_date') || (!$previous && $r['order_by'] == 'post_date') )
  288.                 $posts = array_reverse( $posts, true );
  289.                
  290. //      Option to return something other than the formatted link               
  291.         if ( $r['return'] ) {
  292.                 if ( $r['num_results'] == 1 ) {
  293.                         reset($posts);
  294.                         $post = current($posts);
  295.                         if ( $r['return'] === 'id')
  296.                                 return $post->ID;
  297.                         if ( $r['return'] === 'href')
  298.                                 return get_permalink($post);
  299.                         if ( $r['return'] === 'object')
  300.                                 return $post;
  301.                         if ( $r['return'] === 'title')
  302.                                 return $post->post_title;
  303.                         if ( $r['return'] === 'date')
  304.                                 return mysql2date($r['date_format'], $post->post_date);
  305.                 } elseif ( $r['return'] === 'object')
  306.                         return $posts;
  307.         }
  308.  
  309.         $output = $r['before'];
  310.  
  311. //      When num_results > 1, multiple adjacent posts may be returned. Use foreach to display each adjacent post.
  312.         foreach ( $posts as $post ) {
  313.                 $title = $post->post_title;
  314.                 if ( empty($post->post_title) )
  315.                         $title = $previous ? __('Previous Post') : __('Next Post');
  316.  
  317.                 $title = apply_filters('the_title', $title, $post->ID);
  318.                 $date = mysql2date($r['date_format'], $post->post_date);
  319.                 $author = get_the_author_meta('display_name', $post->post_author);
  320.        
  321. //              Set anchor title attribute to long post title or custom tooltip text. Supports variable replacement in custom tooltip.
  322.                 if ( $r['tooltip'] ) {
  323.                         $tooltip = str_replace('%title', $title, $r['tooltip']);
  324.                         $tooltip = str_replace('%date', $date, $tooltip);
  325.                         $tooltip = str_replace('%author', $author, $tooltip);
  326.                         $tooltip = ' title="' . esc_attr($tooltip) . '"';
  327.                 } else
  328.                         $tooltip = '';
  329.  
  330. //              Truncate the link title to nearest whole word under the length specified.
  331.                 $max_length = intval($r['max_length']) < 1 ? 9999 : intval($r['max_length']);
  332.                 if ( strlen($title) > $max_length )
  333.                         $title = substr( $title, 0, strrpos(substr($title, 0, $max_length), ' ') ) . '...';
  334.        
  335.                 $rel = $previous ? 'prev' : 'next';
  336.  
  337.                 $anchor = '<a href="'.get_permalink($post).'" rel="'.$rel.'"'.$tooltip.'>';
  338.                 $link = str_replace('%title', $title, $r['link']);
  339.                 $link = str_replace('%date', $date, $link);
  340.                 $link = $anchor . $link . '</a>';
  341.        
  342.                 $format = str_replace('%link', $link, $r['format']);
  343.                 $format = str_replace('%title', $title, $format);
  344.                 $format = str_replace('%date', $date, $format);
  345.                 $format = str_replace('%author', $author, $format);
  346.                 if ( ($r['order_by'] == 'custom' || $r['order_by'] == 'numeric') && !empty($r['meta_key']) ) {
  347.                         $meta = get_post_meta($post->ID, $r['meta_key'], true);
  348.                         $format = str_replace('%meta', $meta, $format);
  349.                 } elseif ( $r['in_same_meta'] ) {
  350.                         $meta = get_post_meta($post->ID, $r['in_same_meta'], true);
  351.                         $format = str_replace('%meta', $meta, $format);
  352.                 }
  353.  
  354. //              Get the category list, including custom taxonomies (only if the %category variable has been used).
  355.                 if ( (strpos($format, '%category') !== false) && version_compare(PHP_VERSION, '5.0.0', '>=') ) {
  356.                         $term_list = '';
  357.                         $taxonomies = array_filter( get_post_taxonomies($post->ID), "is_taxonomy_hierarchical" );
  358.                         if ( $r['in_same_format'] && get_post_format($post->ID) )
  359.                                 $taxonomies[] = 'post_format';
  360.                         foreach ( $taxonomies as &$taxonomy ) {
  361. //                              No, this is not a mistake. Yes, we are testing the result of the assignment ( = ).
  362. //                              We are doing it this way to stop it from appending a comma when there is no next term.
  363.                                 if ( $next_term = get_the_term_list($post->ID, $taxonomy, '', ', ', '') ) {
  364.                                         $term_list .= $next_term;
  365.                                         if ( current($taxonomies) ) $term_list .= ', ';
  366.                                 }
  367.                         }
  368.                         $format = str_replace('%category', $term_list, $format);
  369.                 }
  370.  
  371. //              Optionally add the post thumbnail to the link. Wrap the link in a span to aid CSS styling.
  372.                 if ( $r['thumb'] && has_post_thumbnail($post->ID) ) {
  373.                         if ( $r['thumb'] === true ) // use 'post-thumbnail' as the default size
  374.                                 $r['thumb'] = 'post-thumbnail';
  375.                         $thumbnail = '<a class="post-thumbnail" href="'.get_permalink($post).'" rel="'.$rel.'"'.$tooltip.'>' . get_the_post_thumbnail( $post->ID, $r['thumb'] ) . '</a>';
  376.                         $format = $thumbnail . '<span class="post-link">' . $format . '</span>';
  377.                 }
  378.  
  379. //              If more than one link is returned, wrap them in <li> tags              
  380.                 if ( intval($r['num_results']) > 1 )
  381.                         $format = '<li>' . $format . '</li>';
  382.                
  383.                 $output .= $format;
  384.         }
  385.  
  386.         $output .= $r['after'];
  387.  
  388.         //      If echo is false, don't display anything. Return the link as a PHP string.
  389.         if ( !$r['echo'] || $r['return'] === 'output' )
  390.                 return $output;
  391.  
  392.         $adjacent = $previous ? 'previous' : 'next';
  393.         echo apply_filters( "{$adjacent}_post_link_plus", $output, $r );
  394.  
  395.         return true;
  396. }
  397. ?>
clone this paste RAW Paste Data