WordPress.org

Ready to get started?Download WordPress

Forums

Possible bug in previous/next_post_link (7 posts)

  1. vtxyzzy
    Member
    Posted 4 years ago #

    In this thread it was reported that excluding a category did not work when in_same_cat was TRUE.

    On examining the code in wp-includes/link-template.php, I found this line (line 886):

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

    which removes any category ids from $excluded_categories that are in $cat_array (the array of ids that the post is in).

    This prevents you from excluding a category that the post is in. I think the $excluded_categories should be removed from $cat_array instead.

    Am I missing something?

  2. MichaelH
    Member
    Posted 4 years ago #

    Not addressing your question, in fact this trac ticket confuses me even more as to whether the but was fixed.

    http://core.trac.wordpress.org/ticket/6551

    and just FYI
    http://core.trac.wordpress.org/search?q=next_post_link

  3. vtxyzzy
    Member
    Posted 4 years ago #

    Michael,

    I did search Trac, but did not find anything definite about an open ticket on this problem.

    I created a temporary fix by adding a filter to get_previous/next_post_join:

    <?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');
    ?>
  4. randycarl67
    Member
    Posted 4 years ago #

    When I tried the solution from vtxyzzy 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

  5. Shane G.
    Member
    Posted 4 years ago #

    Hi,

    Check with this article:

    http://codex.wordpress.org/Next_and_Previous_Links

    Also check with this plugin:

    http://wordpress.org/extend/plugins/next-page-not-next-post/

    Thanks,

    Shane G.

  6. vtxyzzy
    Member
    Posted 4 years ago #

    See the reply in the original thread.

  7. vtxyzzy
    Member
    Posted 4 years ago #

    Found a bug, and a problem with the implementation in the original code.

    The bug was in the removal of excluded categories from $new_cats. The regex '/$ex,/' matched incorrectly in cases where for example, $ex = '9' and $new_cats = '12,49,9,62'. The result was '12,42'.

    The implementation problem was that $excluded_categories had already been munged before being passed to the filter. The way to get around this is to use negative cat id's for any excluded id's that are in the same category as the post - not pretty, but seems to work. See the example in the code comments.

    <?php
    /* Remove excluded categories from previous/next_post_link join statement
       Using in_same_cat with excluded does not work in WP 2.8.6 (and other versions, too).
       Any category ids in the page's categories are removed from excluded_categories, so
       they are still shown, not excluded.  With this code, excluded categories are removed
       from the join statement.
       However, you must use negative ids for any categories that are in both in_same_cat and
       in excluded categories. For example, if the page is in categories 4,7,12,15 and you want
       to exclude 12 and 44, you would code '-12,44' for excluded_categories.
     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);
             $new_cats = preg_replace('/^,/','',$new_cats);
             if ($new_cats != '') {
                $new_join = preg_replace("/$old_cats/", $new_cats, $new_join);
             }
          }
       }
       //print_r('<p>SAME_CAT: ');print_r($in_same_cat);print_r('</p>');
       //print_r('<p>EXCLUDED: ');print_r($excluded_categories);print_r('</p>');
       //print_r('<p>JOIN: ');print_r(htmlspecialchars($join));print_r('</p>');
       //print_r('<p>NEW JOIN: ');print_r(htmlspecialchars($new_join));print_r('</p>');
    
       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);
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.