Next and Previous Posts of Same Parent

So I can’t give you a complete solution but here are the pieces that I think might help:

  1. Getting parent post ID: wp_get_post_parent_id()https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/

  2. Getting all posts with a particular parent ID. Here’s a WP_Query args to get all posts by parent ID

    $args = array(
        'post_parent' => $parentID,
        'posts_per_page' => -1,
        'orderby' => '???'   // you need to choose how your child posts should be ordered
    );

So you could use the above code to find the parent ID of the current post and then get all the children of that – these would be the ‘siblings’ of the current post.

  1. Use the above code to find next/prev post IDs. Below is my suggestion for code to read all ‘sibling’ posts and figure out next and previous URLs. What this does is cycle through all the sibling posts of the post we’re on looking for the one before and the one after in order to give you a post ID for previous and next posts. If the current post is at the start, then $prevPostID will be false, so you can use that to not display the previous post link. If the current post as at the end, then $nextPostID will be false, so you can prevent the next post link displaying.

This assumes that you’ve built an $args using the suggestion for getting all children of the parent post ID as above.

$currentID = ?? // assume we have the current post ID here

$wpq = new WP_Query( $args );
if ( $wpg->have_posts() ) {
    $prevPostID = false;
    $nextPostID = false;
    $previousInLoop = false;

    while ( $wpq->have_posts() ) {
        if (get_the_ID() == $currentID) {
            $prevPostID = $previousInLoop;
        }
        if ($previousInLoop == $currentID) {
            $nextPostID = get_the_ID();
            break;
        }
        $previousInLoop = get_the_ID();
    }
}

This is a suggestion for how you could make this code work and is untested code. Happy to help if you use it and have further problems.

Note, you could also grab the next/prev title and URL while you were in the loop, rather than having to look them up again later, but I’ll leave that to you.