Title: pre_get_posts and date_query
Last modified: August 22, 2016

---

# pre_get_posts and date_query

 *  [Knappster](https://wordpress.org/support/users/knappster/)
 * (@knappster)
 * [11 years, 10 months ago](https://wordpress.org/support/topic/pre_get_posts-and-date_query/)
 * I’m trying to amend the main query so that when I view an archive year page it
   displays a list of posts by academic year instead. I’m fairly new to WordPress
   so you’ll have to excuse my ignorance when it comes to certain things but the
   solution I’m trying to implement is one using the pre_get_posts hook and the 
   date_query parameters.
 *     ```
       function get_posts_by_academic_year($query) {
       	if ($query->is_main_query() && $query->is_year()) {
       		$query->set('date_query', array(
       			array(
       				'after'     => array(
       					'year' => $year
       					, 'month' => 9
       					, 'day' => 1
       				),
       				'before'    => array(
       					'year'  => $year+1
       					, 'month' => 8
       					, 'day'   => 31
       				),
       				'inclusive' => true
       			)
       		));
       	}
       }
       add_action( 'pre_get_posts', 'get_posts_by_academic_year' );
       ```
   
 * I get nothing but a 404 page, no error messages at all so I have no idea how 
   to debug this or whether I’m going about this the right way.

The topic ‘pre_get_posts and date_query’ is closed to new replies.

## Tags

 * [date_query](https://wordpress.org/support/topic-tag/date_query/)
 * [pre_get_posts](https://wordpress.org/support/topic-tag/pre_get_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [Knappster](https://wordpress.org/support/users/knappster/)
 * Last activity: [11 years, 10 months ago](https://wordpress.org/support/topic/pre_get_posts-and-date_query/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
