Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:52:13+00:00 2026-06-16T00:52:13+00:00

I have my main blog, which lists all posts. I also have a category

  • 0

I have my main blog, which lists all posts.

I also have a category page that lists only posts from 1 category.

Main Blog

  • Post #1
  • Post #2
  • Post #3
  • Post #4
  • Post #5

Category A Page

  • Post #1
  • Post #3
  • Post #4

Category B Page

  • Post #1
  • Post #3
  • Post #5

If a user clicks to look at a post, and then uses the default next/prev link functions, there is no way for WordPress to know which post should be next.

For example, if the user is looking at Post #3, should the next post be #4 or #5? It all depends on where the user came from.

So I wrote the following code to answer this problem and thought I’d share it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:52:14+00:00Added an answer on June 16, 2026 at 12:52 am

    NOTE: I know that this is not the most efficient way to do this, especially if you have thousands of posts. I’d be interested in seeing better ways to get the next post.

    Add the following to your functions.php file:

    /*
     * Session Tracking
     */    
        add_action('init', 'start_session', 1);
        function start_session() {
            if(!session_id()) {
                session_start();       
            }
        }
        // Update Cookie trail
        function update_cookie_trail() {
            $_SESSION['ref_category'] = get_query_var('cat');
        }
    
    
    /*
     * Return next post based off of cookies
     */
        function next_post_from_session($text, $categories) {
            global $post;
    
            $cat_array = explode(',', $categories);
            $cat_array[] = $_SESSION['ref_category'];
    
            // Get all posts, exclude Hidden cat
            $args = array(
                'numberposts'     => -1,
                'category'        => implode(',', $cat_array),
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'post_type'       => 'post',
                'post_status'     => 'publish',
            );
            $allPosts = get_posts( $args );
            $index = 0;
    
            foreach( $allPosts as $thePost ) {
                $index++;
                if($post->ID == $thePost->ID) {
                    $nextPost = $allPosts[$index++];
                    $url = get_permalink($nextPost->ID);
                    $a = '<a href="'.$url.'" title="'.esc_attr($nextPost->post_title).'" />'.$text.'</a>';
                    return $a;
                }
            }
        }
    
    /*
     * Return previous post based off of cookies
     */
        function previous_post_from_session($text, $categories) {
            global $post;
    
            $cat_array = explode(',', $categories);
            $cat_array[] = $_SESSION['ref_category'];
    
            // Get all posts, exclude Hidden cat
            $args = array(
                'numberposts'     => -1,
                'category'        => implode(',', $cat_array),
                'orderby'         => 'post_date',
                'order'           => 'DESC',
                'post_type'       => 'post',
                'post_status'     => 'publish',
            );
            $allPosts = get_posts( $args );
            $index = 0;
    
            foreach( $allPosts as $thePost ) {
                if($post->ID == $thePost->ID) {
                    $prevPost = $allPosts[$index-1];
                    $url = get_permalink($prevPost->ID);
                    $a = '<a href="'.$url.'" title="'.esc_attr($prevPost->post_title).'" />'.$text.'</a>';
                    return $a;                
                }
                $index++;
            }
        }
    
    
    /*
     * Generate a "back" URL to the previous category page based off session data
     */
        function previous_category_permalink() {
            $ref_url = $_SESSION['ref_category'];
            return get_category_link($ref_url);
        }
    

    And then on any category.php or page where you show several blog posts (like a “related posts” section), run this function:

    update_cookie_trail();
    

    Then on your single.php you can use the following functions.

    <?php echo next_post_from_session('Next', '-24, 10'); ?>
    
    <a class="close" href="<?php echo previous_category_permalink(); ?>">
        Back
    </a>                      
    
    <?php echo previous_post_from_session('Previous Post', '-24, 10'); ?>
    

    The ‘-24, 10’ is a parameter that allows you to exclude, or specifically include categories via comma separated ID’s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have blog system which is every user have their own main page like
I'm trying to do blog application and I want main page to have this
I have a main-section div that is set to inherit it's height from its'
I have a category navigation in the wordpress sidebar which contains two main categories
Right now, I have blog posts being generated as nodes in my main content
We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html but what about BSON. What
I have main window which has inner grid components. When I press a button
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
i have main activity in which i have Four menus. and i have one
Splash I have main activity UI startup operations that take between 5-10 seconds (that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.