1

I am using Polylang plugin, which assigns each post to a taxonomy 'language'. While using get_adjacent_post() I want to bring only those posts that belong to the language currently displayed.

So I am writing the following:

$prev_post = get_adjacent_post( true, '', true, 'language' ); $next_post = get_adjacent_post( true, '', false, 'language' );

But this brings the next and previous posts independently from the language they are currently in. Why?

I understand in the documentation $in_same_term (first value) specifies whether post should be in a same taxonomy term. And that $taxonomy is the taxonomy name, if $in_same_term is true. However this is not working in my case, I am getting all languages in previous / next link.

Why I am using adjacent_posts() and not get_next_post() or get_previous_post()?

Because it's mandatory in this case that I am able to sort these next / previous posts by menu_order, something that get_next_post(), get_previous_post() can't do. So I am implementing a filter in functions.php to achieve this.

If it's relevant to this case, in that filter I am specifying this: https://gist.github.com/yoren/81d1ca3a8448256bf65c

1 Answer 1

0

To use get_adjacent_post you have exclude all the term id of that specific taxonomy, For polylang your texonomy is language, First you have retrieve all term ids of indivual languages, and then exclude all of them other than the one you want.

    global $post;
    $post_id = [XXXX]; // any specific post id 
    $post = get_post($post_id);
    //get all the tems of language taxonomy 
    $terms = get_terms( 'language', array(
        'hide_empty' => false,
    ) );
    $excludeds_term_ids = array();
    foreach($terms as $term){
        if($term->slug !='en'){   // exlude all of them other than 'en'
            $excludeds_term_ids[]= $term->term_id; 
        }
    }
    
    
    $next_post = get_adjacent_post(true,$excludeds_term_ids,false,'language');
    echo $next_post->ID;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.