个性化阅读
专注于IT技术分析

如何根据自定义分类,开始日期和结束日期以及在wordpress中的搜索输入来获取帖子?

自定义帖子类型—事件

自定义分类法—– myevents

使用jQuery Datepicker作为开始日期和结束日期

用于搜索事件名称的搜索字段

如何根据自定义分类,开始日期和结束日期以及在wordpress中的搜索输入来获取帖子?1

我想通过使用简单的表单搜索来获取自定义帖子类型事件。在其中, 我在select选项中获取了自定义分类法, 然后在最后一个Search字段中使用jQuery Datepicker添加了开始日期和结束日期。

我的搜索网址如下所示:

http://localhost/customthemedev/?categoryfilter=Techfest&Start=01-May-2019&End=10-May-2019&search=smoke+event+
<form class="select-event-date" method="GET" action="<?php bloginfo('home'); ?>" autocomplete="off" id="filter">
  <div class="form-group eventfunction">
    <label>Categories</label>
    <select class="form-control" name="categoryfilter">
    <?php
       $tax_terms = get_terms('myevents', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):
          echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
    </select> 
  </div>
  <div class="form-group selectDate">
    <input type="text" class="form-control" placeholder="Start Date" name="Start" id="txtFrom">
  </div>
  <div class="form-group endDate">
    <input type="text" class="form-control" placeholder="End Date" name="End"  id="txtTo">
  </div>
  <div class="form-group searchEvent">
    <label>
      <img src="<?php echo get_template_directory_uri();; ?>/assets/images/search-22.png" alt="Icon">
    </label>
    <input type="text" placeholder="Search.." name="search">
  </div>
  <button type="submit" class="btn btn-default">FIND EVENTS</button>
</form>

#1


Try this :

//以数组格式获取所有术语

    $termSlug = array();
    $tax_terms = get_terms('myevents', array('hide_empty' => '0'));      
    foreach ( $tax_terms as $tax_term ):
      $termSlug[] = $tax_term->slug;   
    endforeach;

//Assign the date into variable 

    $start_date = '01-May-2019';
    $end_date ='10-May-2019';  

//use the date format acccording to SQL format

    $start_date_new = date("YOUR DATE FORMAT", strtotime($start_date));
    $end_date_new = date("YOUR DATE FORMAT", strtotime($end_date));
    $event_args = array(
        'post_type' => 'Event', //Post type event  
        'orderby' => 'date', 'order' => 'DESC', 'posts_per_page' => -1, 'meta_query'=>array(    //Meta Query search
            'relation'=>'AND', array(
                'key' => 'start_date', 'value' => $start_date_new, //start date here
                'compare' => '>=', 'type' => 'NUMERIC'
             ), array(
                'key' => 'end_date', 'value' => $end_date_new, //end date here
                'compare' => '<=', 'type' => 'NUMERIC'
             )
         ), 'tax_query' => array(
            array (
                'taxonomy' => 'myevents', //Taxonomy search 
                'field' => 'slug', 'terms' => $termSlug, //terms in array
            )
        ), );
    $all_events = new WP_Query($event_args); 

    echo '<pre>';print_r($all_events);echo '</pre>';
赞(0)
未经允许不得转载:srcmini » 如何根据自定义分类,开始日期和结束日期以及在wordpress中的搜索输入来获取帖子?

评论 抢沙发

评论前必须登录!