<?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::get_context();
$post = new TimberPost();
$context['post'] = $post;

// Relacionados -> general
function get_related_posts($terms) {
    $terms_query = array();
    
    foreach($terms as $term) {
		if ($term->term_id != 1) {
			array_push($terms_query, $term->term_id);
		}
    }
    
    $list_terms_ids = implode(',',$terms_query);
    
    $args = array(
        'post_type' => 'post',
        'numberposts' => 3,
		'tax_query' => array(
			array(
				'taxonomy' => 'category',
				'field'    => 'id',
				'terms'    => array($list_terms_ids),
				'operator' => 'OR',
			)
		),
        'post__not_in' => array( get_the_ID() )
    );
    return Timber::get_posts($args);
    
    
}

//~ $context = Timber::get_context();

//~ $post = Timber::query_post();
//~ $context['post'] = $post;
$context['related'] = get_related_posts($post->get_terms());

if ( post_password_required( $post->ID ) ) {
	Timber::render( 'single-password.twig', $context );
} else {
	Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );
}

//~ $timber_post = Timber::query_post();
//~ $context['post'] = $timber_post;
//~ $context['related'] = get_related_posts($timber_post->get_terms());

//~ 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 );
//~ }
