Solution for pagination in only current post category

I have Next and Prev posts links in the sidebar. Those links are showing on custom post single page. They should be showing next and previous posts from the same category as the current post.

Right now they are using all the posts in that navigation. I’m looking for a way to limit those links range to only the current post category.

  • Dimitris Kalliris
    • Support Team Lead

    Hello there Luke,

    hope you're doing well! :slight_smile:

    I made some adjustments in the

    wp-content/themes/foundationpress_2019-01-16_15-15/template-parts/content-catalog-aside.php

    file and to be exact:

    [attachments are only viewable by logged-in members]

    [attachments are only viewable by logged-in members]

    Please keep in mind that I used the category__in parameter, which is checking for multiple categories but not any children of them.

    https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    Let me know how that goes as it'll need some more testing and I may be missing something.

    Warm regards,

    Dimitris

  • Luke
    • Flash Drive

    Hi Dimitris,

    Thanks for giving it a look. Just to be clear, we have two main tier categories on for our Catalog posts.

    1) Distribution and

    2) Production

    In those main categories, there are sub-categories (Albums, Films, Series, and Specials, for Distributions):

    [attachments are only viewable by logged-in members]

    When entering a subcategory page the pagination click-through should ONLY remain within the respective sub-category.

    An example, if you are on the Series archive page and click title #1:

    (series-catalog-archive (1).png)

    When you click the “Next” pagination it should take you to post #2, or if you click “Prev” it will take you to post #10. But only within the sub-category.

    (pagination-next-and-previous (1).png)

    Maybe is__category won’t work for the child categories?? I was trying the get_adjacent_post function since it checks. . .

    Whether post should be in a same taxonomy term.

    . But had little luck.

    I hope my explanation is a bit clearer. Let me know if you need more info on our setup.

    Thanks again!

    -Luke

    • Luke
      • Flash Drive

      Thanks Dimitris!

      Almost there! In the file (template-parts/content-categories.php) I have the sorting set (per the client’s request) to ascending title -> alphabetical:

      <?php
      // sort posts by title
      // https://www.shilling.id.au/2011/11/30/how-to-change-the-order-of-posts-in-the-wordpress-loop/
      $args = array_merge($wp_query->query, array( 'orderby' => 'title','order' => 'ASC' ));

      query_posts($args);

      if (have_posts($args)) : ?>

      <!-- Start the Loop -->
      <?php while (have_posts()) : the_post();?>

      The code above is sorting all of the category archive pages.

      So then we would need to apply the same rationale to the Category post pagination. Because with your most recent snippet random, unalphabetized categories appear when clicking through the “Next” and “Previous” pagination links.

      Let me iterate again that when you enter the category, click a title/post and then click the “next” or “prev” pagination links. . . you should only see titles/posts IN that category.

      An example for you, if you go to the Distribution/Series page and click the first title(Series Post A in below example), when clicking the “Next” pagination link you will see (for privacy’s sake but also to give the public an example):

      Distribution > Series
      Series Post #1 (alphabetically)
      Series Post #2 (alphabetically)
      Special Post #15 (not sure)
      Special Post #29 (not sure)
      Special Post #67 (not sure)

      ^^^ It jumps right out of the category on the third pagination click. I think this has something to do with the Date published. But not sure. What the pagination click through should be is:

      Distribution > Series
      Series Post #1 (alphabetically)
      Series Post #2 (alphabetically)
      Series Post #3 (alphabetically)
      Series Post #4 (alphabetically)
      Series Post #5 (alphabetically) <- and at the last post in the Series category, return to the 1st post.
      Series Post #1 (alphabetically)

      ** and the opposite when clicking on “Prev”

      I hope this makes sense.

      Please let me know if not.

      What I’m having the most trouble with is querying the sub-category. And only the sub-category.

      Thanks again for your help on this :slight_smile:

      -Luke

  • Dimitris Kalliris
    • Support Team Lead

    Hello there Luke,

    I’m sorry for the late response here, it’s been a bit hectic in our end lately.

    The WP admin credentials you shared during the live chat aren’t working anymore, so I can’t have another look in order to refresh my memory about your setup and actually evaluate if this is something that doesn’t actually surpass the scope of our support.

    https://wpmudev.com/docs/getting-started/getting-support/#chapter-1

    Could you please grant temporary support access via WPMUDEV Dashboard plugin? No need to share any WP admin credentials, just navigate in WP admin area under WPMU DEV -> Support page and click on the “grant support access” button. You can find detailed information about it here: https://wpmudev.com/docs/getting-started/getting-support/#chapter-5

    Please do reply back here when access is granted because we don’t get any notifications about it.

    Thank you,

    Dimitris