• Resolved Mattaton

    (@mattaton)


    I have posts in a custom post type with a custom taxonomy.

    I have 5 total posts, all of which are in term A. 2 of the 5 are in term B. I want to exclude term A from the next and previous post links. I thought it was done like so:

       $exclude_ID = 2; // Term 'A'
        get_previous_post(true, $exclude_ID, 'custom_taxonomy')
        get_next_post(true, $exclude_ID, 'custom_taxonomy')

    What I would expect is it would ignore that the current project belongs to term A and then go to term B and find its sibling in term B. Instead, I’m getting nothing back for either previous or next even though I am certain they are both in term B. So, I’m assuming I have misunderstood how exclusions work or I have something wrong in the code.

    If I remove the exclusion, it will get the next/previous posts for term A, no problem.

    Any ideas?

    Thanks!

    Here is the code in question:

       $exclude = get_term_by( 'slug', 'featured-projects', 'project_types' );
        $exclude_ID = $exclude->term_id;
        $prev_link = get_previous_post_link('%link', '<i class="fas fa-angle-left"></i>previous project', true, $exclude_ID, 'project_types');
        $next_link = get_next_post_link('%link', 'next project<i class="fas fa-angle-right"></i>', true, $exclude_ID, 'project_types');
        if (strlen($prev_link)) {
        	echo $prev_link;
        } 
        if ( strlen($prev_link) && strlen($next_link) ) {
        	echo '<span>|</span>';
        }
        if (strlen($next_link)) {
        	echo $next_link;
        }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You’ve misunderstood. When you provide an exclusion ID, any posts assigned that term are excluded from the search for next or previous, regardless of any other properties. Having term B, C, D makes no difference as long as a post has term A, it is excluded. The function’s exclusion logic is very simplistic, you are trying to instill greater logic to it than it is capable of.

    It is possible to instill more complex logic of your choosing by hooking the “get_{$adjacent}_post_where” filter. $adjacent will be either ‘next’ or ‘previous’, you would hook both versions. This filter manages the SQL WHERE clause used to find next and previous posts. Examine what the current clause says so you can decide on how to modify it.

    The clause will have criteria for date, post type, then it probably has something like term_id NOT IN (2), plus possibly other criteria. If term B ID is 3, I think you need to alter the restriction to something like (term_id NOT IN (2) OR term_id IN (3)). Include with 3 any other IDs which should override the term_id 2 restriction. Take this as an untested example. The final syntax is going to be different, and even the logic may need adjustment.

    In any case this filter is how we instill more complex logic into the nav functions.

    Thread Starter Mattaton

    (@mattaton)

    Ah ha, that makes complete sense now. I should have guessed that’s how it worked since it’s much more versatile than the way I thought it worked. Guess it was just wishful thinking on my part. ??

    Thanks!

    Moderator bcworkz

    (@bcworkz)

    No problem. We’ve all wanted something so bad we couldn’t think straight ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_next_post term exclusion not working as expected’ is closed to new replies.