Advertisement
miriamdepaula

WordPress: More Previous and Next Posts

Nov 11th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | None | 0 0
  1. <?php
  2. /*
  3. This is a function customization to get more than one previous and next post!
  4. Original function can be found at https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/link-template.php#L1465
  5.  
  6. Put the code below into your theme functions.php file.
  7. */
  8.  
  9. function wpmidia_get_adjacent_posts( $post_id = null, $limit = 1, $previous = true, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
  10.     global $wpdb;
  11.  
  12.     if ( ( ! $post = get_post( $post_id ) ) || ! taxonomy_exists( $taxonomy ) )
  13.         return null;
  14.  
  15.     $current_post_date = $post->post_date;
  16.  
  17.     $join = '';
  18.     $posts_in_ex_terms_sql = '';
  19.     if ( $in_same_term || ! empty( $excluded_terms ) ) {
  20.         $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";
  21.  
  22.         if ( $in_same_term ) {
  23.             if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
  24.                 return '';
  25.             $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  26.             if ( ! $term_array || is_wp_error( $term_array ) )
  27.                 return '';
  28.             $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
  29.         }
  30.  
  31.         $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
  32.         if ( ! empty( $excluded_terms ) ) {
  33.             if ( ! is_array( $excluded_terms ) ) {
  34.                 if ( false !== strpos( $excluded_terms, ' and ' ) ) {
  35.                     _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
  36.                     $excluded_terms = explode( ' and ', $excluded_terms );
  37.                 } else {
  38.                     $excluded_terms = explode( ',', $excluded_terms );
  39.                 }
  40.             }
  41.  
  42.             $excluded_terms = array_map( 'intval', $excluded_terms );
  43.  
  44.             if ( ! empty( $term_array ) ) {
  45.                 $excluded_terms = array_diff( $excluded_terms, $term_array );
  46.                 $posts_in_ex_terms_sql = '';
  47.             }
  48.  
  49.             if ( ! empty( $excluded_terms ) ) {
  50.                 $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
  51.             }
  52.         }
  53.     }
  54.  
  55.     $adjacent = $previous ? 'previous' : 'next';
  56.     $op = $previous ? '<' : '>';
  57.     $order = $previous ? 'DESC' : 'ASC';
  58.  
  59.     $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
  60.  
  61.     $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
  62.  
  63.     $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT $limit" );
  64.  
  65.     $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
  66.     $query_key = 'adjacent_post_' . md5( $query );
  67.     $result = wp_cache_get( $query_key, 'counts' );
  68.     if ( false !== $result ) {
  69.         if ( $result )
  70.             $result = array_map( 'get_post', $result );
  71.         return $result;
  72.     }
  73.  
  74.     $result = $wpdb->get_col( $query );
  75.     if ( null === $result )
  76.         $result = '';
  77.  
  78.     wp_cache_set( $query_key, $result, 'counts' );
  79.  
  80.     if ( $result )
  81.         $result = array_map( 'get_post', $result );
  82.  
  83.     return $result;
  84. }
  85.  
  86. /*
  87. Usage: ATTENTION: THIS IS A SAMPLE CODE! CHANGE IT FOR YOUR PURPOSE...
  88. Put the code below where you want to show de list of posts
  89. TIP: You can use print_r() to see what the function is bringing.....
  90.  
  91. PARAMTERS:
  92. $post_id = The ID of the actual post
  93. $limit = Number of posts you want
  94. $previous = If true, it shows the previous posts list. If false, it shows the next posts list.
  95. ...
  96. */
  97.  
  98. echo '<h2>Previous posts</h2>';            
  99. $previous = wpmidia_get_adjacent_posts(19, 3);  // CHANGE THE ID AND NUMBER OF POSTS
  100.  
  101. if( $previous ){
  102.                    
  103.     echo '<ul>';
  104.                    
  105.     foreach( $previous as $prev ){
  106.                          
  107.         echo '<li>[' . mysql2date('d/m/Y', $prev->post_date) . '] ' . $prev->post_title . '</li>'; 
  108.                          
  109.     }
  110.     echo '</ul>';
  111.                    
  112. }
  113.                
  114. echo '<h2>Next posts</h2>';
  115. $next = wpmidia_get_adjacent_posts(19, 3, false); // CHANGE THE ID AND NUMBER OF POSTS
  116.                
  117. if( $next ){
  118.                    
  119.     echo '<ul>';
  120.                    
  121.     foreach( $next as $nextpost ){
  122.                          
  123.         echo '<li>[' . mysql2date('d/m/Y', $nextpost->post_date) . '] ' . $nextpost->post_title . '</li>'; 
  124.                          
  125.     }
  126.     echo '</ul>';
  127.                    
  128. }
  129.  
  130. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement