Jandon
Jandon

Reputation: 625

Timber/Wordpress - next/prev post with args

I have a list with long and short posts.

The short posts are not display in a single page because they are too short. I use an ACF field (checkbox type) to define short post : article_short

But when I'm in a single page who display a long post I would like to display the next/prev long post available.

I wrote :

$context['prev_next_posts'] = Timber::get_posts(array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'meta_query' => array(
        array(
              'key' => 'article_short',
              'compare' => 'LIKE',
              'value' => 0
          )
      ),
    'orderby' => 'date',
    'order'    => 'DESC',
    'has_password' => FALSE
));

Short posts are well excluded.

In my twig file I attached my context :

{% if prev_next_posts.next %}
   <a href="{{ prev_next_posts.next.link }}">{{ prev_next_posts.next.title }}</a>
{% endif %}

{% if prev_next_posts.prev %}
   <a href="{{ prev_next_posts.prev.link }}">{{ prev_next_posts.prev.title }}</a>
{% endif %}

But nothing is display... Do you have any idea please ?

According to Timber documentation I tried to display post in the same category too with (true). Same result. Nothing is display.

https://timber.github.io/docs/reference/timber-post/#next

{% if prev_next_posts.next(true) %}
   <a href="{{ prev_next_posts.next.link }}">{{ prev_next_posts.next.title }}</a>
{% endif %}

{% if prev_next_posts.prev(true) %}
   <a href="{{ prev_next_posts.prev.link }}">{{ prev_next_posts.prev.title }}</a>
{% endif %}

Upvotes: 0

Views: 1375

Answers (1)

Jameel
Jameel

Reputation: 51

Your current problem is that prev_next_posts is just an array of all of your long articles. POST.next and POST.prev are designed to work on a single post object.

Unfortunately get_adjacent_post() is limiting out of the box to only exclude by terms, categories, etc. and not by a meta_key.

Here is a quick way to solve what you're trying to accomplish:

single.php:

// Get all long articles, but only their IDs
$long_articles = get_posts(array(
  'post_type' => 'post',
  'post_status' => 'publish',
  'meta_query' => array(
      array(
        'key' => 'article_short',
        'compare' => 'LIKE',
        'value' => 0
      )
    ),
  'orderby' => 'date',
  'order' => 'DESC',
  'has_password' => FALSE,
  'fields' => 'ids', // Only get post IDs
  'posts_per_page' => -1
));

// Get the current index in the array of long article IDs
$current_index = array_search($post->ID, $long_articles);

// Get the previous post if it exists
if (isset($long_articles[$current_index - 1])) {
  $context['previous_post'] = Timber::get_post($long_articles[$current_index - 1]);
}

// Get the next post if it exists
if (isset($long_articles[$current_index + 1])) {
  $context['next_post'] = Timber::get_post($long_articles[$current_index + 1]);
}

Then in single.twig:

{# Previous post link if it exists #}
{% if previous_post %}
  <div>
    <a href="{{ previous_post.link }}">Previous Post: {{ previous_post.title }}</a>
  </div>
{% endif %}

{# Next post link if it exists #}
{% if next_post %}
  <div>
    <a href="{{ next_post.link }}">Next Post: {{ next_post.title }}</a>
  </div>
{% endif %}

Upvotes: 2

Related Questions