get terms that have post with custom post type between 2 values

This is the finish code for this:

<?php 

    $dates_array = array();
    $countries_array = array();
    $term_list = array();
    $current_term_list = array();

    $args = array( 'post_type' => 'tours', 'posts_per_page' => -1 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    global $post;

    $tour_terms = wp_get_post_terms( $post->ID, 'tours_cats' );
    $current_term_list = array();
    foreach ($tour_terms as $tour) {

        if(!array_key_exists( $tour->term_id , $term_list )) {
            $term_list[$tour->term_id] = array();
        }
        $current_term_list[] = $tour->term_id;
    }
    $tour_dates="";
    if( have_rows('tour_dates_repeater') ):
    while ( have_rows('tour_dates_repeater') ) : the_row();
        $mydate = get_sub_field('date_start');
        $date = DateTime::createFromFormat('dmY', $mydate);

        foreach($current_term_list as $term_dates) {
            $my_custom_date = $date->format('Ymd');
            if(!in_array($my_custom_date, $term_list[$term_dates])) {
                $term_list[$term_dates][] = $date->format('Ymd');
            }

        }
    endwhile;
    endif; //end repeater field

endwhile; wp_reset_query(); //end wp_query ?>

<?php 

echo '<select name="dest">';
    foreach($term_list as $key => $value) {
        $values="";
        foreach($value as $date) {
            $values .= '.date_'. $date .' ';
        }
        $term_name = get_term_by('id', $key, 'tours_cats')->name;
        echo '<option value="'. $key .'" class="'. $values .'">'. $term_name .'</option>';
    }
echo '</select>';


$termslist = array();
foreach($term_list as $key => $value) {
    $termslist = $termslist + $value;
}
$flip = array_flip($termslist);


foreach($flip as $key_var => $var) {
    $flip[$key_var] = array();
}

foreach($flip as $key_var => $var) {

    foreach($term_list as $key => $value) {

        if(in_array($key_var, $value)) {
            $flip[$key_var][] = $key;
        }
    }

}

echo '<select name="dest">';
    foreach($flip as $key => $date_select) {
        $values="";
        foreach($date_select as $termid) {
            $values .= '.termid_'. $termid .' ';
        }
        echo '<option value="'. $key .'" class="'. $values .'">'. $key .'</option>';
    }
echo '</select>';
?>

Leave a Comment