WordPress.org

Support

Support » How-To and Troubleshooting » pre_get_posts and date_query

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.
Skip to toolbar