0

I have taxonomy terms for Countries and post type for Destination that have many dates for tour.
I want to get all terms that have posts width the Custom Filed (Custom field with Advanced custom field pro plugin) that have values between two values.

Example:
I have 8 tours dates(custom field) to china (post) in Asia (taxonomy term) and 2 tours dates(custom field) to England (post) in Europe (taxonomy term) between 1.6.2016 and 31.6.2016.

I want to get those two terms Asia and Europe.

Edit:

1) I need to display all tour(it post) dates in tour single page, and create taxonomy for countries.

2) I need to filter posts by dates and by taxonomy term

3) I need to create two drop down list (select) to filter. when you choose one select, the other select changes to only available. If you select India the dates select change to all available dates from all post from taxonomy India.

  • I'd start with a meta_query to get posts within those dates and then loop the posts to get the terms from the taxonomy. while you could combine with a tax_query it could make it more complex than you need. – majick Jun 4 '16 at 5:16
  • but that mean that I need to query all posts from the site. it's can take long time to over all posts and get all their terms and filter that. – Guy Ytzhak Jun 4 '16 at 12:27
0

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>';
?>
  • you should really be storing your dates as y/m/d and doing a meta_query on the date rather than querying for everything and filtering in PHP. if you eventually have lots of tours, you may reach a point where it no longer fits in physical memory. – Milo Jun 5 '16 at 0:31
  • but I need array of all dates. the dates is array of terms id that have those dates. So by meta query I can't get this. I can get post, but here I need to get terms. – Guy Ytzhak Jun 5 '16 at 10:09
  • I would say you should rethink how you're storing that data then. – Milo Jun 5 '16 at 15:59
  • @Milo What are you think about it? How do you store those things? 1) I need to display all tour(it post) dates in tour single page, and create taxonomy for countries. 2) I need to filter posts by dates and by taxonomy term 3) I need to create two drop down list (select) to filter. when you choose one select, the other select changes to only available. If you select India the dates select change to all available dates from all post from taxonomy India. – Guy Ytzhak Jun 5 '16 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.