Support » Fixing WordPress » query next and previous post link

  • This is one of those question where i’m pretty sure something is possible, but I don’t know what to research to learn how to do it. I was hoping someone could give me some key words to Google to point me in the right direction.

    Basically, on my front page I am using WP_Query() to pull a custom post type and then sort them by both date and time (I use Custom Metaboxes and Fields by WebDevStudios on the backend).

    When a person clicks on a single post I would like the user to be able to click through using the previous and next links for only that specific date metabox data and then sorted by time as well. I was able to use plugin called “Ambrosite Next/Previous Post Link Plus” to pass arguments to a previous and next link to only query a specific custom post type as well as those that have a matching metabox field. This almost did it. I am able to click through my CPT that have a matching date. However, this plugin will not allow me to pass another argument so that I can sort by time as well. So, back to the drawing board.

    Can someone point me in the direction of how I can do this? The arguments that you can pass to next_post_link() and previous_post_link() seem limited.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can add a parameter to previous/next_post_link() with a couple of filters. I only show the code for the next_post_link() filter, you need to create a similar one for previous_post_link(). Here is the code:

    function mam_next_post_link_filter ($link='') {
      global $mam_global_stay_in_cat;
      // echo '<p>NEXT LINK BEFORE:' . htmlspecialchars($link) . '</p>';
      if ($mam_global_stay_in_cat && $link) {
        $link = mam_add_query_arg('stayincat',$mam_global_stay_in_cat,$link);
        // echo '<p>NEXT LINK AFTER:' . htmlspecialchars($link) . '</p>';
      }
      return $link;
    }
    add_filter('next_post_link','mam_next_post_link_filter');
    
    function mam_add_query_arg ($key,$value,$link) {
       // Adds the parameter $key=$value to $link, or replaces it if already there.
       // Necessary because add_query_arg expects an href, not a complete link.
       if (strpos($link,'href')) {
         $hrefpat = '/(href *= *([\"\']?)([^\"\' ]+)\2)/';
       } else {
         $hrefpat = '/(([\"\']?)(http([^\"\' ]+))\2)/';
       }
       if (preg_match($hrefpat,$link,$matches)) {
          $url = $matches[3];
          $newurl = add_query_arg($key,$value,$url);
          // echo '<p>OLDURL:' . htmlspecialchars($url) . '</p>';
          // echo '<p>NEWURL:' . htmlspecialchars($newurl) . '</p>';
          $link = str_replace($url,$newurl,$link);
       }
       return $link;
    }

    You need the mam_add_query_arg() function because the $link parameter is a complete link (inside an <a ...>text</a> tag), and add_query_arg() only works on hrefs or raw URLs.

    Hope that is clear enough.
    To use this, you define a global variable containing your value just prior to the call to previous/next_post_link() and reset the variable to false after the call.

    Thread Starter WPWanderer

    (@wpwanderer)

    Hey, thanks a lot. I’ve been reading up on filters. I will work through your answer.

    Thanks again.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query next and previous post link’ is closed to new replies.