Support » Fixing WordPress » Can’t exclude a category from the previous_post_link and next_post_link

  • Resolved randycarl67

    (@randycarl67)


    I am unable to exclude a category from the previous_post_link and next_post_link in single.php.

    The category I want to exclude is id # 1 – named “View All”.

    The View All category is the default category for all posts.

    Every post is in 2 categories; a specific category like “Baby & Children” as well as the View All category.

    Here is the Previous and Next link code in single.php.

    <?php previous_post_link(‘%link’, ‘Previous’, TRUE, ‘1’); ?>
    <?php next_post_link(‘%link’, ‘Next’, TRUE, ‘1’); ?>

    I need to not show the “View All” category just the items specific category.

Viewing 15 replies - 1 through 15 (of 25 total)
  • I wonder if the fact that all posts are in the View All category is causing the problem because of the TRUE for in_same_cat.

    Try removing the View All from one post and see if the links work for that post.

    Yup works just fine – If I remove the View All category from a specific category like Baby & Children.

    Now I’d really like to have a “View All” category…Is there anyway to have a “View All” category or a “Show All”? The website is very visual with every post being an image with a description and “View All” is a great way to show off.

    I would say that may be a bug in WP. At line 886 in wp-includes/link-template.php there is this:

    $excluded_categories = array_diff($excluded_categories, $cat_array);

    where the categories that the post is in ($cat_array) are taken out of $excluded_categories. I think it should be the other way around. That is, excluded categories should be removed from the list that the post is in.

    If you want to hack the core code, try changing this (a few lines earlier) CAUTION UNTESTED:

    if ( $in_same_cat ) {
       $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
       $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
    }

    to this:

    if ( $in_same_cat ) {
       $cat_array = wp_get_object_terms($post->ID, 'category', 'fields=ids');
       if (!empty($excluded_categories)) {
          $cat_array = array_diff($cat_array,$excluded_categories);
       }
       if (!empty($cat_array) ) {
          $join .= " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
       }
    }

    I think I have a temporary solution. Paste the code below onto the end of your functions.php file. It adds a filter to take the excluded post id(s) out of the join statement.

    <?php
    // Remove excluded categories from previous/next_post_link join statement
    // Author: Mac McDonald
    // Contact: Use 'Contact Us' form under 'About' at bluegrassmiataclub.com
    function pnp_link_joinfilter ($join,$in_same_cat,$excluded_categories) {
       $new_join = $join;
       if ($in_same_cat && $excluded_categories) {
          preg_match('/\(((\d+,?)+)\)/',$join,$matches);
          if ($matches) $old_cats = $matches[1];
          $new_cats = $old_cats . ',';
          foreach ($excluded_categories as $ex) {
             $new_cats = preg_replace("/$ex,/",'',$new_cats);
          }
          $new_cats = preg_replace('/,$/','',$new_cats);
          if ($new_cats != '') {
             $new_join = preg_replace("/$old_cats/", $new_cats, $new_join);
          }
       }
       return $new_join;
    }
    add_filter('get_previous_post_join','pnp_link_joinfilter');
    add_filter('get_next_post_join','pnp_link_joinfilter');
    ?>

    Note that if you try to remove all the categories that the post is in, nothing is changed.

    Thank you for the help.

    However when I add the temp solution to the functions.php file, I get a php error:

    Fatal error: Call to undefined function add_filter() in /home/info/my-website.com/www/wp-includes/functions.php on line 3661

    <?php
    // Remove excluded categories from previous/next_post_link join statement
    // Author: Mac McDonald
    // Contact: Use 'Contact Us' form under 'About' at bluegrassmiataclub.com
    function pnp_link_joinfilter ($join,$in_same_cat,$excluded_categories) {
       $new_join = $join;
       if ($in_same_cat && $excluded_categories) {
          preg_match('/\(((\d+,?)+)\)/',$join,$matches);
          if ($matches) $old_cats = $matches[1];
          $new_cats = $old_cats . ',';
          foreach ($excluded_categories as $ex) {
             $new_cats = preg_replace("/$ex,/",'',$new_cats);
          }
          $new_cats = preg_replace('/,$/','',$new_cats);
          if ($new_cats != '') {
             $new_join = preg_replace("/$old_cats/", $new_cats, $new_join);
          }
       }
       return $new_join;
    }
    add_filter('get_previous_post_join','pnp_link_joinfilter');
    add_filter('get_next_post_join','pnp_link_joinfilter');
    ?>

    What version of WP are you using? It was tested for 2.8.6. Also, add it to the end of your theme’s functions.

    I’m on 2.9.1. And it was at the very end of the functions.php.

    But the error message says it is at the end of wp-includes/functions.php, not wp-content/themes/mytheme/functions.php.

    Well – Duh! I guess I was looking at this so long that I missed the simple instructions for mytheme/functions.php.

    I did add it to mythemes/functions.php and it completely broke the website. Nothing would load, just blank.

    Sorry for being such a bother. And thank you!

    Well, I am sorry. I don’t have a way to test on 2.9.1 at present.

    What theme are you using? I can at least test that in 2.8.6.

    OK – after a few adjustments, it should work now. I apologize for the lack of earlier testing.

    One other change: to get the excluded ids passed through link-template.php, code them as negatives. So, to exclude category ‘1’, code it as ‘-1’ whenever you use in_same_cat as TRUE.

    Please try this version:

    <?php
    // Remove excluded categories from previous/next_post_link join statement
    // Code excluded categories as negative values eg '-15 and -4'.
    // Author: Mac McDonald
    // Contact: Use 'Contact Us' form under 'About' at bluegrassmiataclub.com
    function pnp_link_joinfilter ($join = null,$in_same_cat = false,$excluded_categories = '') {
       $new_join = $join;
       if ($join) {
          if ($in_same_cat && $excluded_categories) {
             preg_match('/\(((\d+,?)+)\)/',$join,$matches);
             if ($matches) $old_cats = $matches[1];
             $new_cats = $old_cats . ',';
             foreach ($excluded_categories as $ex) {
                $ex = abs($ex);
                $new_cats = preg_replace("/$ex,/",'',$new_cats);
             }
             $new_cats = preg_replace('/,$/','',$new_cats);
             if ($new_cats != '') {
                $new_join = preg_replace("/$old_cats/", $new_cats, $new_join);
             }
          }
       }
    
       return $new_join;
    }
    add_filter('get_previous_post_join','pnp_link_joinfilter',10,3);
    add_filter('get_next_post_join','pnp_link_joinfilter',10,3);
    ?>

    Sorry but this doesn’t work in WP 2.9.1. With that code the website is completely broken. Blank. Nothing. Busted.

    I am confused. It works perfectly for me in WP 2.8.6 and in 2.9.1, which I just installed. I even loaded your theme to rule out theme problems.

    Can you post the end of your functions.php file including the last 10 or so lines above this code?

    This is without your fix….

    function mooz_image_predata($post_id) {
    $post_id = (int) $post_id;
    if(!$post_id) return false;
    $max_width = wptp_getopt(‘img_width’, 0);
    $max_height = wptp_getopt(‘img_height’, 0);
    $switch = wptp_getopt(‘tim_url’, 0);
    $image[‘id’] = wptp_get_image_attachment($post_id);
    if(empty($image[‘id’])) return false;
    if(empty($switch))
    $image[‘path’] = mooz_get_base_path($image[‘id’]);
    else
    $image[‘path’] = mooz_get_base_url($image[‘id’]);
    $image[‘size’] = mooz_get_ratio($image[‘id’], $max_width, $max_height);
    if(false == $image[‘size’])
    return false;
    return $image;
    }

    add_action(‘admin_menu’, ‘mooz_fotofolio_init’);

    ?>

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘Can’t exclude a category from the previous_post_link and next_post_link’ is closed to new replies.