<?php



$args = array();
$sites = get_sites($args);

$listaPosts = array();
foreach ($sites as $site) {
    switch_to_blog($site->blog_id);
  
    $argsSite = array(
        'post_type'=>array('posts', 'u99RedesSociales'),
        'post_status' => 'publish',
        'meta_query' => array(
            'relation' => 'OR'
        )
    );
    foreach ($tipo as $value) {
        $argsSite['meta_query'][]=
            array(
                'key' => 'tipo',
                'value' => $value,
                'compare' => '='
            );
    }
  
    $loop = new WP_Query($argsSite);
  
    if ($loop->have_posts()) :
        $prio = 0;
      while ($loop->have_posts()) : $loop->the_post();
        $listaPosts[] = array('site_id'=>$site->blog_id,'id'=>get_the_ID(),'fecha'=>get_the_date('YmdHis'),'prioridad'=>$prio);
        $prio++;
      endwhile;
    endif;
  
    restore_current_blog();
}

//print_r($listaPosts);

$fechas = array_column($listaPosts, 'fecha');
$prioridades = array_column($listaPosts, 'prioridad');

array_multisort($prioridades, SORT_ASC, $fechas, SORT_DESC, $listaPosts);
//print_r($listaPosts);

$fechaPost = array();
if(array_search(get_the_date('Ymd'), $fechas)){
    
}

//die();
 
if ( sizeof($listaPosts) > 0 ) {
    ?>
    
    <div id="tarjeta<?php echo $name; ?>" class="linkInicio col-md-12 col-lg-6 col-xl-4">

        <div class="tarjetaContenido tarjetaContenido<?php echo $name; ?> alto_ventana_tarjeta">
            <div id="cabezal<?php echo $name; ?>" class="cabezalTarjetas cabezalTarjetas<?php echo $name; ?>">
                <h1><?php echo $titulo; ?></h1>
            </div>
            <div id="carousel<?php echo $name; ?>" class="carousel slide"  data-interval="false" data-ride="false" data-touch="true">
                <div class="carousel-inner">
            <?php
            // Load posts loop.
            $i = 0;
            $fuente = [];
            $max = 10;
            foreach ($listaPosts as $p) {
                switch_to_blog($p['site_id']);
                global $post;
                $post = get_post( $p['id']);
                setup_postdata($post); 
                $categories = get_post_meta( get_the_ID(), 'tipo',true );
                $end = strrpos($categories,"-");
                $categories = substr($categories,0,$end);
                $fuente[$i] = $categories;

                include( locate_template( 'template-parts/content/content.php', false, false ) );
                
                $i++;
                restore_current_blog();
                if($i >= $max){
                    break;
                }
            }
            wp_reset_postdata();

            ?>
                    <ol class="carousel-indicators">
                        <?php
                        for($j=0;$j<$i;$j++){
                            ?><li data-target="#carousel<?php echo $name; ?>" data-slide-to="<?php echo $j; ?>" class="<?php echo $fuente[$j]; ?><?php if($j===0){ echo ' active';} ?>"></li><?php
                        }
                        ?>
                    </ol>
                </div>
            
            </div>
            <div class="content-controls-carrusel" >
                <a class="carousel-control-prev" href="#carousel<?php echo $name; ?>" role="button" data-slide="prev">
                    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                    <span class="sr-only">Previous</span>
                </a>
                <a class="carousel-control-next" href="#carousel<?php echo $name; ?>" role="button" data-slide="next">
                    <span class="carousel-control-next-icon" aria-hidden="true"></span>
                    <span class="sr-only">Next</span>
                </a>
            </div>
        </div>
    </div>
  <?php
}


