<?php 
define('WP_USE_THEMES', false);  
require_once('../../../wp-load.php');

if(isset($_GET["values"])){

	$values = $_GET["values"];

	$the_query;

	$contador = 0;

	$portfolioRows = array();

	$largoFiltros = count($values);

	
	if($largoFiltros == 1){

		$the_query = new WP_Query( array(

		    'post_type'              => array( 'portfolio' ),
		    'nopaging'               => true,
		    'paged'                  => '10',
		    'posts_per_page'         => '10',
		    'orderby'                => 'title',
		  

		) );

	}

	else if($largoFiltros == 2){

		$value = $values[0];
		$type = $values[1];
		
		$the_query = new WP_Query( array(

	    'post_type'              => array( 'portfolio' ),
	    'nopaging'               => true,
	    'paged'                  => '10',
	    'posts_per_page'         => '10',
	    'orderby'                => 'title',
	    'meta_key'               =>  $type,
	    'meta_value'             =>  $value,	  

		) );


	} else if($largoFiltros == 4){

		$value = $values[0];
		$type = $values[1];

		$value2 = $values[2];
		$type2 = $values[3];

		$the_query = new WP_Query( array(

		    'post_type'              => array( 'portfolio' ),
		    'nopaging'               => true,
		    'paged'                  => '10',
		    'posts_per_page'         => '10',
		    'orderby'                => 'title',
		   
		    'meta_query'	=> array(
				
				'relation'		=> 'AND',
				
				array(
					'key'		=> $type,
					'value'		=> $value,
					'compare'	=> '='
				),

				array(
					'key'		=> $type2,
					'value'		=> $value2,
					'compare'	=> '='
				)
			)
	  

		) );


	}else if($largoFiltros == 6){

		$value = $values[0];
		$type = $values[1];

		$value2 = $values[2];
		$type2 = $values[3];

		$value3 = $values[4];
		$type3 = $values[5];


		$the_query = new WP_Query( array(

		    'post_type'              => array( 'portfolio' ),
		    'nopaging'               => true,
		    'paged'                  => '10',
		    'posts_per_page'         => '10',
		    'orderby'                => 'title',
		   
		    'meta_query'	=> array(
				
				'relation'		=> 'AND',
				
				array(
					'key'		=> $type,
					'value'		=> $value,
					'compare'	=> '='
				),

				array(
					'key'		=> $type2,
					'value'		=> $value2,
					'compare'	=> '='
				),

				array(
					'key'		=> $type3,
					'value'		=> $value3,
					'compare'	=> '='
				)
			)
	  

		) );

	}


	 if ( $the_query->have_posts() ) {
 
   
    	while ( $the_query->have_posts() ) : $the_query->the_post(); 

    	     $portfolioRows[$contador][0] = 1; 
        	
    		 $portfolioRows[$contador][1] = get_the_title();

        	 $actividad = get_field( "actividad" );
             $portfolioRows[$contador][2] = $actividad;


             $pais = get_field( "pais" );
             $portfolioRows[$contador][3] = $pais;


             $ano = get_field( "ano" );
             $portfolioRows[$contador][4] = $ano;

             $selected = get_field('link');

             if( is_array($selected) && in_array('si', $selected) ) {

             	$portfolioRows[$contador][5] = "no";

             }else{

             	$portfolioRows[$contador][5] = get_the_permalink();

             }


             $contador++;

    	endwhile;
    
    wp_reset_postdata(); 

    $vector = json_encode($portfolioRows);

    echo $vector;

	}else{

	 $portfolioRows[0][0] = 0; 
	 $portfolioRows[0][1] = "No hay obras para los filtros seleccionados, pruebe eligiendo otras opciones";

	 $vector = json_encode($portfolioRows);

    echo $vector;

	};


}else{

	echo "no se encontraon resultados.";
}




?>