How do you choose to display specific posts/pages by parent url

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-labellarr; 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-labellarr; 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-labelNext 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-labelNext Post rarr;/p
            h4 class=adjacent-title?php echo get_the_title(); ?/h4
        /div
    /a
    ?php wp_reset_postdata();
}

Topic pagination Wordpress

Category Web


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.