使用筛选器进行日期和类别查询

时间:2015-09-18 作者:MrFox

我对此有点困惑。我有一个查询,可以查找类别和日期内的帖子。

我可以筛选类别,但我不知道如何按日期筛选。

查询工作正常。我看到了我查询过的所有帖子,但我不知道如何过滤日期。如果我知道url中要显示的内容,这会有所帮助

搜索类别返回/博客/?类别=促销

日期筛选结果应该是什么样的?

<?php $categorys = get_terms( \'category\', array( \'hide_empty\' => true, \'fields\' => \'all\' ) ); ?>
    <form class="staff-filter" method="GET" action=""><div class="col-sm-12 text-center">
    <span>Filter Posts by:</span>
      <ul class="list-inline">
       <li>
          <label>
            <select name="category">
              <option value="" disabled selected> Category </option>
                    <?php foreach( $categorys as $category ) : ?>
              <option value="<?php echo $category->slug; ?>">
                    <?php echo $category->name; ?>
              </option>
                    <?php endforeach; ?>
            </select>
          </label>
       </li>      

      <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date DESC");

              $months = $wpdb->get_col("SELECT DISTINCT MONTHNAME(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date DESC");?>

            <li>
          <select name="date_start">
            <option value="" disabled selected> Date </option>
                    <?php foreach($months as $month) : ?>
            <option> <?php echo \'<ul><li class"list-unstyled"><a href="\'. site_url() .\'/\'.$year .\'/\'.date(\'m\', strtotime($month)).\'"/> \' . $month .\'</a></li></ul>\';?></option>
            <?php endforeach; ?>

            <?php foreach($years as $year) : ?>
            <option><?php echo \'<ul><li class"list-unstyled"><a href="\'. site_url() .\'\'.$year.\'"/> \' . $year .\'</a></li></ul>\';?></option>
            <?php endforeach; ?>   
           </select>
        </li>
      </ul>
  <!-- SUBMIT BUTTON -->      
  <button class="btn"><span class="glyphicon glyphicon-search" aria-hidden="true">      </span> Search</button>
  <!-- END SUBMIT BUTTON -->
 </div>
 </form>

 <?php

    $cat_query = array(array(\'relation\' => \'AND\'));

    if( isset( $_GET[\'category\'] ) && $_GET[\'category\'] ){

            $cat_area_query = array( \'taxonomy\' => \'category\', \'field\' => \'slug\', \'terms\' => $_GET[\'category\'],\'operator\' => \'IN\', );
            $cat_query[] = $cat_area_query;
    }

    if( $cat_query && $cat_query ){
            $cat_query[\'relation\'] = \'AND\'
            ;
    }

    $args = array(
                    \'post_type\'      => array(\'post\'),
                    \'post_status\'    => \'publish\',
                    \'tax_query\'      => $cat_query,
                    \'orderby\'        => \'date\',
                    \'order\'          => \'desc\',
                    \'date_query\' => array(
                    \'relation\' => \'OR\',
            array(
                    \'year\'  => $getdate[\'year\'],
                    \'month\' => array(9, 8,7, 6, 5),
                    \'compare\'   => \'=\',
            ),
            ),
    );?>
    $posts_query = new WP_Query( $args );?>


 <?php if( $posts_query->have_posts() ) : ?>

 <?php while( $posts_query->have_posts() ) : $posts_query->the_post(); ?>

<div class="ms-item col-lg-6 col-md-6 col-sm-6 col-xs-12">

    <?php if (has_post_thumbnail()) : ?>

        <figure class="article-preview-image">

            <a href="<?php the_permalink(); ?>" class="post-title-link"><?php the_post_thumbnail(); ?></a>

        </figure>

    <?php else : ?>

    <?php endif; ?>
     <a href="<?php the_permalink(); ?>">  
    <div class="post-content white">
    <?php $category = get_the_category(); ?>
            <span class="post-category"><?php echo $category[0]->cat_name;?></span>
        <span class="post-date"><i class="fa fa-clock-o"></i> <?php the_time(\'F j, Y\') ?></span>
        <h2 class="post-title"><?php the_title(); ?></h2>

    <?php the_excerpt(); ?>
    </div>
    </a>

 <?php endwhile;?>

Link to pastebin

2 个回复
最合适的回答,由SO网友:MrFox 整理而成

我的想法是错误的。我需要允许用户先选择日期,然后选择类别。

我还需要使下拉框自动选择,以便首先执行日期搜索,将用户带到日期存档,然后用户可以选择他们需要的类别。

<form class="my-filter" method="GET" action=""><div class="col-sm-12 text-center">
        <span>Filter Posts by:</span>
      <ul class="list-inline">
     <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date DESC");
      $months = $wpdb->get_col("SELECT DISTINCT MONTHNAME(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date DESC");?>
        <li>
          <select onChange="window.location.href=this.value">
                <option value="" disabled selected> Date </option>
                    <?php foreach($months as $month) : ?>
                <option value="<?php echo site_url() .\'/\'.date(\'Y\') .\'/\'.date(\'m\', strtotime($month))?>"> <?php echo \'<ul><li class"list-unstyled">\' . $month .\'</li></ul>\';?></option>
                    <?php endforeach; ?>

                    <?php foreach($years as $year) : ?>
                <option value="<?php echo  site_url() .\'/\'.$year ?>"><?php echo \'<ul><li class"list-unstyled">\' . $year .\'</li></ul>\';?></option>
                    <?php endforeach; ?>    
            </select>
        </li>
        <li>
          <label>
            <select onChange="window.location.href=this.value">
                <option value="category" disabled selected> Category </option>
                    <?php foreach( $categorys as $category ) : ?>
                <option value="?category=<?php echo $category->slug; ?>">
                    <?php echo $category->name; ?>
                </option>
                <?php endforeach; ?>
            </select>
            </label>
        </li>       
      </ul>   
      </div>
    </form>
现在,最终url为

http://mywebsite.com/2015/09/?category=special-events
要确保查询仅找到当月的类别,$args必须包含如下动态date\\u查询:

$m = get_the_time(\'m\');
$y = get_the_time(\'Y\');

$args = array(
            \'post_type\'      => array(\'post\'),
            \'post_status\'    => \'publish\',
            \'tax_query\'      => $cat_query,
            \'orderby\'        => \'date\',
            \'order\'          => \'desc\',
            \'date_query\' => array(
    array(
        \'year\'  => $y,
        \'month\' => $m,
    ),
),  

);

SO网友:tiadotdev

对于任何希望按日期过滤而不使用URL组件的人,我最终将此用于表单:

<!-- Filter Form -->
<form method="GET" action="/research-submissions/" id="submissionsFilterForm">
    <div class="filter-form--content">
        <div class="filter-items">

            <!-- Years -->
            <div class="filter-item year">
                <?php $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type = \'post\' ORDER BY post_date DESC"); ?>
                <select name="years[]">
                    <option disabled selected><?php _e(\'Year\', \'twentytwentyone\'); ?></option>
                    <?php foreach($years as $year) : ?>
                        <option 
                            value="<?= $year; ?>"
                            <?php echo selected(
                                (isset($_GET[\'years\']) && in_array($year, $_GET[\'years\']))
                            ) ?>
                        ><?= $year; ?></option>
                        <?php endforeach; ?>
                </select>
            </div>
            
        </div>  
        <div class="category-filters--submit">
            <button type="submit" name=""><?php _e(\'Search\', \'twentytwentyone\'); ?></button>
        </div>
    </div>
</form> 
对于带有args的wp\\u查询:

<!-- Results -->
<div class="submissions-content">
            
    <?php 
    
    // Years
    if($_GET[\'years\'] && !empty($_GET[\'years\'])) {
      $yearsF = $_GET[\'years\'];
      foreach  ($yearsF as $yearF) {
          $year_filter = $yearF;
      }
    }
    
    global $post;

    $date_query = array(\'relation\' => \'AND\');
    if( isset($_GET[\'years\']) ) {
        $date_query[] =  array(
            \'year\'     => $year_filter,
        );
    }
            
    $args = array(
        \'post_type\'         => array(\'post\'),
        \'posts_per_page\'    => -1,
        \'date_query\'        => $date_query,
    );
            
    $query = new WP_Query($args);
        if ( $query->have_posts() ) : 
            while($query -> have_posts()) : $query -> the_post();

                $title = get_the_title($post->ID); 
                $date = get_the_date( \'F j, Y\' ); ?>

                <div class="single-post submission">
                  <h3><?= $title; ?></h3>
                  <time><?= $date; ?></time>
                </div>
            
            <?php endwhile;
            
            else : ?>

                <div class="single-post error">
                    <?php _e(\'Oops! Nothing matches your search\', \'twentytwentyone\'); ?>. <span><?php _e(\'Try again\', \'twentytwentyone\'); ?></span>.
                </div>

        <?php endif;
            
        wp_reset_query(); ?>
            
</div>

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post