File: //var/www/linde-ai/wp-content/themes/linde/single.php
<?php
/**
 * The Template for displaying all single posts
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since    Timber 0.1
 */
$context     = Timber::context();
$timber_post = Timber::get_post();
$context['post'] = $timber_post;
if($timber_post->post_type == "post"){
    $timber_post     = new PostNewsFlexible();
    $context['post'] = $timber_post;
    $context_sections = array();
    $context['articles_page_link'] = get_field('articles_page', 'options');
    $long_articles = get_posts(array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'orderby' => 'date',
        'order' => 'DESC',
        'has_password' => FALSE,
        'fields' => 'ids', // Only get post IDs
        'posts_per_page' => -1
      ));
    // Filter out posts where post_visibility is 'lva-platform'
    $long_articles = array_filter($long_articles, function ($post_id) {
        $visibility = get_post_meta($post_id, 'post_visibility', true);
        return empty($visibility) || $visibility !== 'lva-platform';
    });
    // Re-index the array after filtering
    $long_articles = array_values($long_articles);
    
    // Get the current index in the array of long article IDs
    $current_index = array_search($post->ID, $long_articles);
    
    // Get the previous post if it exists
    if (isset($long_articles[$current_index - 1])) {
    $context['previous_post'] = Timber::get_post($long_articles[$current_index - 1]);
    }
    
    // Get the next post if it exists
    if (isset($long_articles[$current_index + 1])) {
    $context['next_post'] = Timber::get_post($long_articles[$current_index + 1]);
    }
    Timber::render( array('single.twig' ), $context );
}else if($timber_post->post_type == "linde_course"){
    
    $timber_post = Timber::get_post(false, "PostCourse");
    $comingSoon = $timber_post->meta( 'coming_soon' );
    $allowViewing = $timber_post->meta( 'allow_viewing_of_full_course_page' );
    
    $context['current_url'] = Timber\URLHelper::get_current_url();
    
    if( $comingSoon && ! $allowViewing && $timber_post->post_type == "linde_course") {
        Timber::render( '404.twig', $context );
    } else {
        $terms = $timber_post->terms( 'linde_module' );
        $session_number = $timber_post->meta('session_number');
        if(count($terms) > 0){
            $context['term'] = new Timber\Term($terms[0]->ID);
            $term_id = $terms[0]->ID;
            $args = array(
                'post_type' => 'linde_course',
                'posts_per_page' => 50,
                'orderby' => 'date',
                'order'   => 'DESC',
                'meta_query'	=> array(
                    'relation'		=> 'OR',
                    array(
                        'key'	  	=> 'session_number',
                        'value'	  	=> $session_number - 1,
                        'compare' 	=> '=',
                    ),
                ),
                'tax_query' => array(
                    array(
                        'taxonomy' => 'linde_module',
                        'field'    => 'term_id',
                        'terms'    => $term_id,
                    ),
                ));
            $previous_course = Timber::get_posts($args, "PostCourse");
            $args = array(
                'post_type' => 'linde_course',
                'posts_per_page' => 50,
                'orderby' => 'date',
                'order'   => 'DESC',
                'meta_query'	=> array(
                    'relation'		=> 'OR',
                    array(
                        'key'	 	=> 'session_number',
                        'value'	  	=> $session_number + 1,
                        'compare' 	=> '=',
                    ),
                ),
                'tax_query' => array(
                    array(
                        'taxonomy' => 'linde_module',
                        'field'    => 'term_id',
                        'terms'    => $term_id,
                    ),
                ));
            $next_course = Timber::get_posts($args, "PostCourse");
            if($previous_course && count($previous_course) > 0) {
                $context['previous_course'] = $previous_course[0];
            }
            if($next_course && count($next_course) > 0) {
                $context['next_course'] = $next_course[0];
            }
        }
        if ( post_password_required( $timber_post->ID ) ) {
            Timber::render( 'single-password.twig', $context );
        } else {
            Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
        }   
        
    }
    
} else {
 Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', 'single.twig' ), $context );
}