Изменение массива/формы для фильтрации пользовательского типа сообщений WordPress по двум таксономиям одновременно

#php #arrays #wordpress

Вопрос:

У меня есть курсы пользовательского типа с двумя таксономиями — Местоположение и Продукт. Ниже шаблон (добавлен в конец вопроса) показывает их с формой фильтрации. Эта форма отлично работает до тех пор, пока я не попытаюсь добавить в игру другую таксономию course_location .

Я изменил массив вот так:

 if(1) {
    if( $category_filter ) {
        $arg_tax_query[] = array (
        'post_type'  => 'course',
        'relation' => 'OR',
            array (
            'taxonomy' => 'course_location',
            'field' => 'term_id',
            'terms' => $category_filter,
        ),
        array (
            'taxonomy' => 'course_product',
            'field' => 'term_id',
            'terms' => $category_filter,
        ),
    );
    }
    else {
        $arg_tax_query = array ();
    }
}
 

и заменил $search_location; поле ввода формы на это дублированное и измененное <select> :

             <div class="select-group select-border event-filter-box">
                <select name="category_filter" id="category_filter" onchange="this.form.submit()">
                    <option value="">Select Location</option>
                    <?php  
                        $terms = get_terms('course_location');
                        foreach($terms as $term){
                            $seleted = '';
                            if( $category_filter == $term->term_id ) { 
                                $seleted = ' selected="selected"'; 
                            }
                            echo '<option value="'.$term->term_id.'" '.$seleted.'>'.$term->name.'</option>'; 
                        }
                    ?>
                </select>
            </div>
 

Теперь форма работает только для фильтрации course_product , но не course_location . Пожалуйста, посоветуйте, как реализовать фильтрацию обеих таксономий. Весь первоначальный рабочий шаблон состоит из:

     <?php
/**
* Template Name: Course Listing Template
*/

get_header();

global $post;
$heading = get_field('heading', get_the_ID() );
$sub_heading = get_field('sub_heading', get_the_ID() );

?>

<!-- content area part -->
<div class="main-content">
    <?php 
    $search_text = isset( $_GET['search_text'] ) ?  $_GET['search_text'] : '';
    $search_location = isset( $_GET['search_location'] ) ?  $_GET['search_location'] : '';
    $category_filter = isset( $_GET['category_filter'] ) ?  $_GET['category_filter'] : '';
    $start_date = isset( $_GET['start_course_date'] ) ?  $_GET['start_course_date'] : '';
    $end_date = isset( $_GET['end_course_date'] ) ?  $_GET['end_course_date'] : '';
    $pager = isset( $_GET['pager'] ) ?  $_GET['pager'] : '1';
    
    $arg_meta_query = array();
    $arg_tax_query = array();

    
    $newStartDate = date("Ymd", strtotime($start_date));
    $newEndDate = date("Ymd", strtotime($end_date));
    

    if($search_location) {
        $arg_meta_query[] = array(
            'key' => 'course_location',
            'value' => $search_location,
            'compare' => 'LIKE',
        );
    }

    if($start_date amp;amp; $end_date) {
        $arg_meta_query[] = array(
            'relation' => 'OR',
            array(
                'key' => 'course_start_date',
                'value' => array( $newStartDate, $newEndDate ),
                'compare' => 'BETWEEN'
            ),
            array(
                'key' => 'course_end_date',
                'value' => array( $newStartDate, $newEndDate ),
                'compare' => 'BETWEEN'
            )
        );
    } else {
        $today = date( 'Ymd' );
        $arg_meta_query[] = array(
            'relation' => 'OR',
            array(
                'key' => 'course_start_date',
                'value' => $today,
                'compare' => '>='
            ),
            array(
                'key' => 'course_end_date',
                'value' => $today,
                'compare' => '>='
            )
        );
    }


    if(1) {
        if( $category_filter ) {
            $arg_tax_query[] = array (
                'taxonomy' => 'course_product',
                'field' => 'term_id',
                'terms' => $category_filter,
            );
        } else {
            $arg_tax_query = array ();
        }
    }
    


    $posts_per_page = 12;
    $args = array(
        'post_type' => array( 'course' ),
        's' => $search_text,
        'meta_key' => 'course_start_date',
        'orderby' => 'meta_value_num',
        'order' => 'ASC',
        'post_status' => 'publish',                 
        'posts_per_page' => $posts_per_page,
        'tax_query' => $arg_tax_query,
        'paged' => $pager,
        '_meta_or_title' => $search_location,
        'meta_query' => $arg_meta_query,
    );
        
    ?>
<form action="<?php echo get_permalink($post->ID); ?>" autocomplete="off" method="GET" id="event_list_frm">
    <section class="event-filter">
        <div class="container">
        <div class="event-filter-wrap">
            
            <?php  
                $search_text = isset( $_GET['search_text'] ) ?  $_GET['search_text'] : '';
                $search_location = isset( $_GET['search_location'] ) ?  $_GET['search_location'] : '';
                $category_filter = isset( $_GET['category_filter'] ) ?  $_GET['category_filter'] : '';
                $start_date = isset( $_GET['start_course_date'] ) ?  $_GET['start_course_date'] : '';
                $end_date = isset( $_GET['end_course_date'] ) ?  $_GET['end_course_date'] : '';
                
            ?>
                <div class="search-input event-filter-box search-grp">
                    <input type="search" name="search_text" id="search_text" value="<?php echo $search_text; ?>" placeholder="Search">
                    <button class="icon-search search-btn"></button>
                </div>
                <div class="search-input event-filter-box search-grp">
                    <input type="search" name="search_location" id="search_location" value="<?php echo $search_location; ?>" placeholder="Location">
                    <button class="icon-search search-btn"></button>
                </div>
                <div class="select-group select-border event-filter-box">
                    <select name="category_filter" id="category_filter" onchange="this.form.submit()">
                        <option value="">Select Product</option>
                        <?php  
                            $terms = get_terms('course_product');
                            foreach($terms as $term){
                                $seleted = '';
                                if( $category_filter == $term->term_id ) { 
                                    $seleted = ' selected="selected"'; 
                                }
                                echo '<option value="'.$term->term_id.'" '.$seleted.'>'.$term->name.'</option>'; 
                            }
                        ?>
                    </select>
                </div>
                <div class="date-input event-filter-box">
                    <input type="text" name="start_event_date" id="start_event_date" value="<?php echo $start_date; ?>" placeholder="mm/dd/yyyy">
                </div>
                <div class="search-text event-filter-box">
                    <span>to</span>
                </div>
                <div class="date-input event-filter-box">
                    <input type="text" name="end_event_date" id="end_event_date" value="<?php echo $end_date; ?>" placeholder="mm/dd/yyyy">
                </div>
                <div class="search-btn event-filter-box form-submit">
                <a class="btn" href="<?php echo get_permalink($post->ID); ?>" value="Reset Filter">Reset Filter</a>
                </div>
                <input type="hidden" name="pager" id="pager" value="<?php echo $pager; ?>" >            
        </div>
        </div>
    </section>
</form>

<section class="post-listing-section spacing-medium" id="event-listing-section">
    <div class="container">
        <?php   
        $WP_Query = new WP_Query( $args );
        
        if ( $WP_Query->have_posts() ) { 
        
            if( $pager == 1) {
                $start = 1;     
            } else {
                $start = (( $pager-1) * $posts_per_page )  1;                       
            }
            
            if( $WP_Query->max_num_pages == 1 ) {
                $end = $WP_Query->found_posts;
            } elseif( $WP_Query->max_num_pages == $pager ) {
                $end = $WP_Query->found_posts;
            } else {
                $end = ( $pager) * $posts_per_page ;                        
            }

        ?>
        <div class="list-filter d-flex justify-content-end align-items-center">
            
            <div class="showing-count">Showing <?php echo $start; ?>-<?php echo $end; ?> of <?php echo $WP_Query->found_posts; ?></div>
        </div>

        <div class="post-listing-grid">
            <div class="row">                               
                <?php
                while ( $WP_Query->have_posts() ) {
                    $WP_Query->the_post(); 
                    $post_id = get_the_ID();
                    $course_start_date = get_field('course_start_date', get_the_ID() );
                    $course_end_date = get_field('course_end_date', get_the_ID() );
                    $course_location = get_field('course_location', get_the_ID() );
                    $course_product = get_field('course_product', get_the_ID() );
                    $post_type = get_post_type( $post_id );
                    $listing_label = get_field('listing_label', $post_id );
                    $listing_label_color = get_field('listing_label_color', $post_id );
                    
                    $featured_img_url = get_the_post_thumbnail_url( $post_id ,'full'); 
                    
                    $description = get_the_excerpt( $post_id );
                    $length = 100;
                    if(strlen( $description)<=$length ) {
                        $description =  $description;
                    } else {
                        $description = substr( $description,0,$length ) . '...';
                    }
                    
                    $post_tags = get_the_terms( get_the_ID(), 'course_product' );
                ?>
                <div class="cell-lg-4 cell-sm-6 listing-cell <?php if( !$featured_img_url) { echo ' no-image '; } ?>">
                    <div class="listing-inner bg-white">
                        <?php if( $listing_label ) { ?><span class="tag <?php echo $listing_label_color; ?>"><?php echo $listing_label; ?></span>
                        <?php } ?>
                       <?php if( $featured_img_url ) { ?>
                       <figure>
                            <img src="<?php echo $featured_img_url; ?>" alt="">
                        </figure>
                       <?php } ?>
                        <div class="desc">

                            <?php
                            $tag_array = array();
                            if ( ! empty( $post_tags ) ) {
                                foreach( $post_tags AS $tag) {
                                    $tag_array[] = $tag->name;
                                }  
                                echo '<div class="post-tag text-orange">'.implode(", ",$tag_array).'</div>';
                            }

                            ?>
                            
                            <h5><a href="<?php echo get_permalink( $post_id ); ?>"><?php echo get_the_title( $post_id ); ?></a></h5>
                            
                            <?php if( $course_location ) { ?><address><?php echo $course_location; ?></address><?php } ?>
                            
                            <?php if( $description ) { ?><p><?php echo $description; ?></p><?php } ?>
                            
                            <?php 
                            if( $course_start_date ) { 
                                $format_in = 'F d, Y';
                                $format_out = 'd - m'; 
                                
                                $start_date = date("M. d", strtotime($course_start_date));
                                $end_date = date("M. d, Y", strtotime($course_end_date));
                            
                            ?>
                            <div class="event-date"><?php echo $start_date; ?>
                            <?php if( $end_date ) {  ?>     
                            – <?php echo $end_date; ?>
                            <?php } ?>
                            </div>
                            <?php } ?>
                        </div>
                    </div>
                </div>
                <?php } wp_reset_postdata(); ?>
            </div>
        </div>
        
        <?php if( $WP_Query->max_num_pages > 1 ) { ?>
        <div class="pagination">
            <?php if( $pager== 1 ) { ?>
            <a href="javascript:void(0);" class="prev icon icon-circle-arrow-left"  data-text="prev" disable></a>
            <?php } else { ?>
            <a href="javascript:void(0);" class="prev icon icon-circle-arrow-left event-list-prev"  data-text="prev"></a>
            <?php } ?>
            <span class="page"><?php echo $pager; ?></span>
            <a class="page">of</a>
            <span class="page"><?php echo $WP_Query->max_num_pages; ?></span>
            
            <?php if( $WP_Query->max_num_pages == $pager ) { ?>
            <a href="javascript:void(0);" class="next icon icon-circle-arrow-right" data-text="next"disable ></a>
            <?php } else { ?>
            <a href="javascript:void(0);" class="next icon icon-circle-arrow-right event-list-next" data-text="next"></a>
            
            <?php } ?>
        </div>
        <?php } ?>
        
    </div>
    <?php } else {
        echo '<div class="container"> ';
        echo 'No result found.';
        echo '</div>';
    } ?>
</section>