I am a little confused by this. I have a query that finds posts within a category and date.
I can filter the categories, but I have no idea how to filter by date.
The query works. I see all of the posts I have queried, but I don’t know how to filter the date. It would help if I knew what was meant to appear in the url
The search for categories returns /blog/?category=promotions
What should a date filter result look like?
<?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;?>