Query certain amount of posts from multiple dates

You’re just about there. Each date_query array represents a separate condition to filter by. By default, these are combined using AND which limits the scope further. In your case, you want to change the relation to OR and then add the different years as their own separate conditions in the array.

$args  = array(
    'date_query' => array(
        'relation' => 'OR',
        array(
            'year' => '2018',
        ),
        array(
            'year' => '2015',
        ),
    ),
);

The resulting mysql statement looks like this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND ( YEAR( wp_posts.post_date ) = 2018 OR YEAR( wp_posts.post_date ) = 2015 ) AND wp_posts.post_type="post" AND (wp_posts.post_status="publish") ORDER BY wp_posts.post_date DESC LIMIT 0, 10

The important bit is ( YEAR( wp_posts.post_date ) = 2018 OR YEAR( wp_posts.post_date ) = 2015 )

Leave a Comment