Support » Fixing WordPress » exclusion categories in get_adjacent_post just NOT WORKING in WP 2.5

  • 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?

Viewing 15 replies - 1 through 15 (of 16 total)
  • 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')

    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?

    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…

    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.

    Just saw this…

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

    Whoops, I didn’t see #6528, but I just posted a patch to

    @ filosofo

    thanks a lot 😉 you made my day,

    this days and so much days in the past…

    regards Monika

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

    Wow, thank you Filosofo!

    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”.

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

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

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

    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.


    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.

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘exclusion categories in get_adjacent_post just NOT WORKING in WP 2.5’ is closed to new replies.