Get next and prev post by post ID

Hi there,

How to get next and prev post by post ID? 

By default

get_next_post() and get_previous_post()

looking for posts by closest date. 

Is there any way to get next and prev post by the closest post ID?

My code:

      $prev_post = get_previous_post();
      if($prev_post) {
        $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
        echo "\t" . '<a class="prev-photo" rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '"><i class="fa fa-chevron-left" aria-hidden="true"></i></a>' . "\n";
      }
      $next_post = get_next_post();
        if($next_post) {
        $next_title = strip_tags(str_replace('"', '', $next_post->post_title));
        echo "\t" . '<a class="next-photo" rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '"><i class="fa fa-chevron-right" aria-hidden="true"></i></a>' . "\n";
      }

Much love

Answers 0

Related Questions