How inside new WP_Query to display the ID of the first and last custom post? This is required to loop navigation prev and next

Here is the function code without layout.

 $args = array( 'p' => $postID, 'post_type' => 'portfolio-site' ); $recent = new WP_Query($args); while ( $recent->have_posts() ) : $recent->the_post(); if( get_previous_post (true) ) { $first = get_previous_post()->ID; } else { $first = }; $prevPost = $first; if( get_next_post (true) ) { $last = get_next_post()->ID; } else { $last = }; $nextPost = $last; 

I tried to use get_boundary_post , but could not get it to work. Thank you in advance for any help!

    1 answer 1

    It was possible to solve the problem in this way:

     if( get_previous_post() ) { $first = get_previous_post()->ID; } else { $argsfirst = array( 'numberposts' => 1, 'orderby' => 'id', 'order' => 'DESC', 'post_type' => 'portfolio-site' ); $postfirst = get_posts( $argsfirst ); $first = $postfirst[0]->ID; }; $prevPost = $first; if( get_next_post() ) { $last = get_next_post()->ID; } else { $argslast = array( 'numberposts' => 1, 'orderby' => 'id', 'order' => 'ASC', 'post_type' => 'portfolio-site' ); $postlast = get_posts( $argslast ); $last = $postlast[0]->ID; }; $nextPost = $last; 

    I do not know if this is correct, but the problem is solved