0

I have adjacent posts set up correctly but it's displaying all of the posts/pages. I need it to show specific ones. The easiest way would be by parent URL if that's possible as I need to do this multiple times. So www.example.com/sydney/ would be the URL and every page that uses the sydney parent would be displayed and nothing else. It doesn't have to be this way if there is a better/easier way then I am all ears.

/**
 *  Infinite next and previous post looping in WordPress
 */

// Previous post if present, otherwise the very first post.

$prev_post = get_adjacent_post( false, '', true );

if ( $prev_post ) { // previous post
    if ( has_post_thumbnail( $prev_post->ID ) ) {
        $prev_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id( $prev_post->ID ), 'thumbnail' );
    } else {
        $prev_post_image_url = 'https://dummyimage.com/150';
    } ?>

    <a class="prev-post adjacent-post" href="<?php the_permalink( $prev_post->ID ) ?>">
        <img src="<?php echo $prev_post_image_url; ?>" alt="Previous Post" />
        <div>
            <p class="adjacent-label">&larr; Previous Post</p>
            <h4 class="adjacent-title"><?php echo get_the_title( $prev_post->ID ); ?></h4>
        </div>
    </a>
<?php } else { // first post
    $args = array(
        'posts_per_page' => 1,
        'no_found_rows' => true,
        'order' => 'DESC',
    );

    $first = new WP_Query( $args );

    $first->the_post();

    // first post's image URL
    if ( has_post_thumbnail() ) {
        $first_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id(), 'thumbnail' );
    } else {
        $first_post_image_url = 'https://dummyimage.com/150';
    } ?>

    <a class="prev-post adjacent-post" href="<?php the_permalink() ?>">
        <img src="<?php echo $first_post_image_url; ?>" alt="Previous Post" />
        <div>
            <p class="adjacent-label">&larr; Previous Post</p>
            <h4 class="adjacent-title"><?php echo get_the_title(); ?></h4>
        </div>
    </a>
    <?php wp_reset_postdata();
}
    

// Next post if present, otherwise the very last post.

$next_post = get_adjacent_post( false, '', false );

if ( $next_post ) { // next post
    if ( has_post_thumbnail( $next_post->ID ) ) {
        $next_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id( $next_post->ID ), 'thumbnail' );
    } else {
        $next_post_image_url = 'https://dummyimage.com/150';
    } ?>

    <a class="next-post adjacent-post" href="<?php the_permalink( $next_post->ID ) ?>">
        <img src="<?php echo $next_post_image_url; ?>" alt="Next Post" />
        <div>
            <p class="adjacent-label">Next Post &rarr;</p>
            <h4 class="adjacent-title"><?php echo get_the_title( $next_post->ID ); ?></h4>
        </div>
    </a>
<?php } else { // last post
    $args = array(
        'posts_per_page' => 1,
        'no_found_rows' => true,
        'order' => 'ASC',
    );

    $last = new WP_Query( $args );

    $last->the_post();

    // last post's image URL
    if ( has_post_thumbnail() ) {
        $last_post_image_url = wp_get_attachment_image_url( get_post_thumbnail_id(), 'thumbnail' );
    } else {
        $last_post_image_url = 'https://dummyimage.com/150';
    } ?>

    <a class="next-post adjacent-post" href="<?php the_permalink() ?>">
        <img src="<?php echo $last_post_image_url; ?>" alt="Next Post" />
        <div>
            <p class="adjacent-label">Next Post &rarr;</p>
            <h4 class="adjacent-title"><?php echo get_the_title(); ?></h4>
        </div>
    </a>
    <?php wp_reset_postdata();
}

1 Answer 1

1

Use the filters available in get_adjacent_post() to add a post parent condition to the WHERE clause:

function wpse_406178_adjacent_post_where_sibling( $where, $in_same_term, $excluded_terms, $taxonomy, $post ) {
    global $wpdb;

    if ( $post->post_parent ) {
        $where .= $wpdb->prepare( ' AND p.post_parent = %d', $post->post_parent );
    }

    return $where;
}

add_filter( 'get_next_post_where',     'wpse_406178_adjacent_post_where_sibling', 10, 5 );
add_filter( 'get_previous_post_where', 'wpse_406178_adjacent_post_where_sibling', 10, 5 );

Add it to your functions.php

3
  • Hi first of all thanks so much for getting back to me. So I add this to the above code which part would I change to the parent? I would guess I change wpse_406178 should be the parent? Thanks again Danny May 31, 2022 at 8:27
  • The parent would be /sydney/ by the way Thanks Danny May 31, 2022 at 8:58
  • @5StarCruises can you elaborate? I thought that you wanted this to work for all pages/posts that are child of sydney, no? This code automatically uses the assigned parent of the post you're searching adjacent siblings for. May 31, 2022 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.