Date Query Month and Year or only Year
-
I am a bit stuck with setting up this date query. I want to use a dropdown select box to allow the user to choose which date archive they want to view. It works for the months, but when I select the year option it will only show 1 month from that year. I am also allowing the category to be queried, but I don’t think that is affecting the date query.
I know that I’m going wrong somewhere, but I can’t pinpoint the problem exactly.
<?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' ; } $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( 'relation' => 'OR', array( 'year' => $y, 'month' => $m, 'compare' => '=', ), ), ); $posts_query = new WP_Query( $args);?>
- The topic ‘Date Query Month and Year or only Year’ is closed to new replies.