I solved this issue thanks to Tom J Nowell He gave me the right idea in his comment. Everything is really ease (but I didn’t think of it myself) in order to get the identifiers of the previous and next post, it is enough to include them into ajax request. This is how it looks:
<?php
$post = get_post($post_id);
$previous_post = get_previous_post();
$next_post = get_next_post();
?>
<div data-next-id="<?php echo $next_post->ID; ?>" data-prev-id="<?php echo $previous_post->ID; ?>" data-new-id="<?php echo get_the_ID(); ?>" class="news-item">
In the post card, two attributes are created in which the ID of the previous and next post are passed.
Now slightly modified js code:
if(prevPost !== null){
prevPost.addEventListener('click', function(){
let prevAttribute = this.getAttribute('data-prev-id');
$.ajax({
type : 'POST',
url : wp_helper.ajax_url,
data : {
postid : prevAttribute,
action : 'loadprevpost',
},
success : function( data ){
$('#exampleModal .modal-body').html(data);
lazyLoad();
}
});
})
}
And php function:
add_action( 'wp_ajax_loadprevpost', 'really_loadprevpost' );
add_action( 'wp_ajax_nopriv_loadprevpost', 'really_loadprevpost' );
function really_loadprevpost() {
$postid = $_POST[ 'postid' ];
$post = get_post($postid );
setup_postdata( $post );
if (have_rows('news_content',$postid)) {
echo'<div class="news-single">';
while (have_rows('news_content',$postid)) : the_row();
if (get_row_layout() == 'section_title'){
/// Post data here
}
endwhile;
echo'</div>';
}
wp_reset_postdata();
wp_die();
}
Perhaps the solution is not elegant, but it works, for me this is the main thing now.