get adjacent post wordpress

Solutions on MaxInterview for get adjacent post wordpress by the best coders in the world

showing results for - "get adjacent post wordpress"
Sacha
02 Jul 2016
1$prev_post = get_adjacent_post();
2if( $prev_post )
3	echo '<a href="'. get_permalink($prev_post->ID) .'">'. $prev_post->post_title .'</a>';
Carlton
30 Nov 2016
1$next_post = get_adjacent_post(0, '', 0);
2if( $next_post )
3	echo '<a href="'. get_permalink($next_post->ID) .'">'. $next_post->post_title .'</a>';
Alessia
17 Feb 2017
1/*
2 * Replacement for get_adjacent_post()
3 *
4 * This supports only the custom post types you identify and does not
5 * look at categories anymore. This allows you to go from one custom post type
6 * to another which was not possible with the default get_adjacent_post().
7 * Orig: wp-includes/link-template.php 
8 * 
9 * @param string $direction: Can be either 'prev' or 'next'
10 * @param multi $post_types: Can be a string or an array of strings
11 */
12function mod_get_adjacent_post($direction = 'prev', $post_types = 'post') {
13    global $post, $wpdb;
14
15    if(empty($post)) return NULL;
16    if(!$post_types) return NULL;
17
18    if(is_array($post_types)){
19        $txt = '';
20        for($i = 0; $i <= count($post_types) - 1; $i++){
21            $txt .= "'".$post_types[$i]."'";
22            if($i != count($post_types) - 1) $txt .= ', ';
23        }
24        $post_types = $txt;
25    }
26
27    $current_post_date = $post->post_date;
28
29    $join = '';
30    $in_same_cat = FALSE;
31    $excluded_categories = '';
32    $adjacent = $direction == 'prev' ? 'previous' : 'next';
33    $op = $direction == 'prev' ? '<' : '>';
34    $order = $direction == 'prev' ? 'DESC' : 'ASC';
35
36    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
37    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type IN({$post_types}) AND p.post_status = 'publish'", $current_post_date), $in_same_cat, $excluded_categories );
38    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );
39
40    $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
41    $query_key = 'adjacent_post_' . md5($query);
42    $result = wp_cache_get($query_key, 'counts');
43    if ( false !== $result )
44        return $result;
45
46    $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
47    if ( null === $result )
48        $result = '';
49
50    wp_cache_set($query_key, $result, 'counts');
51    return $result;
52}
Marcie
09 Nov 2018
1<?php
2$prev = mod_get_adjacent_post('prev', array('post', 'custom1', 'custom2'));
3$next = mod_get_adjacent_post('next', array('post', 'custom1', 'custom2'));
4?>
5
6<?php if($prev) : ?>
7    <a href="<?php echo get_permalink($prev->ID)?>">« Go back in time</a>
8<?php endif; ?>
9
10<?php if($next) : ?>
11    <a href="<?php echo get_permalink($next->ID)?>">Next: <?php echo $next->post_title; ?> »</a>
12<?php endif; ?>