exclusion categories in get_adjacent_post just NOT WORKING in WP 2.5 (17 posts)

  1. corpodibacco
    Posted 7 years ago #

    get_adjacent_post is the function WP now uses to have previous_post_link and such functions working.

    Anyway the ability to exclude a category when spewing the link is broken for me.

    To reproduce this problem just try having previous_post_link exclude one of the categories, like such:

    previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '2')
    (or whatever category you want to exclude: it will be simply ignored.)

    The culprit is get_adjacent_post, but i find it totally complicated now so I just couldn't fix it.

    Any ideas?

  2. Austin Matzko
    Posted 7 years ago #

    When you pass arguments to a function like previous_post_link, you need to leave out the variable names. For example, the line you have above should be
    previous_post_link('« %link', '%title', false, '2')

  3. corpodibacco
    Posted 7 years ago #

    filosofo, I know. That was just sloppy copy and paste from the function, my bad.
    I am actually a plugin developer so I know a little php.
    Don't just assume it is a mistype or some basic misunderstanding we are talking about here.
    Have you tried the function? Have you found the exclusion of categories working?

  4. corpodibacco
    Posted 7 years ago #

    so anybody has any idea? basically next/previous post link are not capable to exclude categories, that's the gist in case you missed it...

  5. TylerMartin
    Posted 7 years ago #

    Yeah, I've had the same problem and it's really annoying.

    A theme I've developed works great in 2.3, not in 2.5. The excluded_categories parameter is totally broken and I can't seem to find any real acknowledgement of it. I had posted over in the themes section, I'm surprised more theme writers have not come across this, it's one of only 4 parameters of the main navigation system of WordPress.

    It would seem a shame to have to write separate navigation functions just for a theme because the built in ones stopped working the way they were intended.

  6. TylerMartin
    Posted 7 years ago #

    Just saw this...


    I hope it's not until 2.6 that this is fixed.

  7. Austin Matzko
    Posted 7 years ago #

    Whoops, I didn't see #6528, but I just posted a patch to http://trac.wordpress.org/ticket/6551

  8. Monika
    Posted 7 years ago #

    @ filosofo

    thanks a lot ;) you made my day,

    this days and so much days in the past...

    regards Monika

  9. TylerMartin
    Posted 7 years ago #

    Yes, much thanks Filosofo, you're my hero for the day as well.

  10. taote
    Posted 7 years ago #

    Wow, thank you Filosofo!

  11. TylerMartin
    Posted 7 years ago #

    One problem with 6551/fix_get_adjacent_post.diff

    Even with that fix, WP 2.5 doesn't behave quite the same as previous WordPress versions.

    If your excluded category is "dog", it will skip navigating to a post who's category is "dog". However, if the post has more than one category, like "dog" and "cat", then it will not skip that post. So it is counting the post as not being in the "dog" category simply by having a category assigned to it besides "dog".

  12. taote
    Posted 7 years ago #

    Yes, it doesn´t work properly. Have you find any solution TylerMartin?

  13. TylerMartin
    Posted 7 years ago #

    No sorry. And the ticket got closed. I reopened it so maybe someone can track down the remaining problem.

  14. stratoextra
    Posted 6 years ago #

    Anything new here? It seems the flaw that TylerMartin describes persists in 2.7.
    Too bad.

  15. kanden
    Posted 6 years ago #

    1. I know this is bad form
    2. This issue might be resolved in another forum
    3. This is not a solution just a thought

    I'm using 2.7 and I changed line #887 of link-template.php

    $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode($excluded_categories, ',') . ')';

    $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')';

    It now works as a include cats parameter.

  16. kanden
    Posted 6 years ago #


    I think the issue is in the section:

    $excluded_categories = array_map('intval', explode(' and ', $excluded_categories));
    if ( !empty($cat_array) ) {
    $excluded_categories = array_diff($excluded_categories, $cat_array);
    $posts_in_ex_cats_sql = '';

    When I use the parameter like this 1,2,3 and by pass the above section, everything works fine IN or NOT IN

    Again, Just my thoughts, not a solution for production.

  17. Freshish
    Posted 6 years ago #

    Hi fellas,

    I've been looking for a solution all day, and damn is to say, I didn't find nothing.

    I'd like to display only links to posts from the same subcategory, but it doesn't seem to be working.

    I've investigated, and couldn't go further than get_adjacent_post in link-template.php.

    I echoed $posts_in_ex_cats_sql to see what was in there and got:
    AND tt.taxonomy = 'category' AND tt.term_id NOT IN (5)

    which seems actually to be fine as 5 is, in this case, the one category I don't want. (and in the other case, the "excluded" category is correctly 6).

    But, it doesn't work: I'm still given the link to the next post, which is in the very category I don't want.

    I've got version 2.7.x, from what I understand the problem has been here for a long time, and still not fixed, how come?

    And, has somebody any clue how to fix that? It doesn't seem to be too complicated, but I'm just not good enough in php to go further.

    Thanks a lot for your help

Topic Closed

This topic has been closed to new replies.

About this Topic