Skip to main content
fix typo
Source Link
jaswrks
  • 851
  • 5
  • 8

You can use before and after, see date arguments.

$args = array(
    'posts_per_page' => '-1',
    'date_query'     => array(
        array(
            'after'  => 'December 31st, 2015', // i.e., 2016+.
            'before' => 'January 1st, 2018', // i.e., before 2018.
        ),
    ),
);
$query = new WP_Query( $args );

Based on an article written by one of the date query committers, it looks like you can use AND OR logic date queries too. So you can also do this.

$args = array(
    'posts_per_page'  => '-1',
    'date_query'      => array(
        'relation'    => 'OR',
        array( 'year' => 2016 ),
        array( 'year' => 2017 ),
    ),
);
$query = new WP_Query( $args );

All possible arguments according to that author.

'date_query' => array(
    'column' => 'optional, column to query against, default is post_date',
    'compare' => 'optional, see WP_Date_Query::get_compare()',
    'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND',
    array(
        'column' => 'see above',
        'compare' => 'see above',
        'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
        'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
        'inclusive' => 'boolean, for after/before, whether exact value should be matched or not',
        'year' => '4 digit int',
        'month' => 'int, 1-12',
        'week' => 'int, 0-53',
        'day' => 'int, 1-31',
        'hour' => 'int, 0-23',
        'minute' => 'int, 0-60',
        'second' => 'int, 0-60',
    ),
    array(
        ...
    ),
    ..
),

You can use before and after, see date arguments.

$args = array(
    'posts_per_page' => '-1',
    'date_query'     => array(
        array(
            'after'  => 'December 31st, 2015', // i.e., 2016+.
            'before' => 'January 1st, 2018', // i.e., before 2018.
        ),
    ),
);
$query = new WP_Query( $args );

You can use before and after, see date arguments.

$args = array(
    'posts_per_page' => '-1',
    'date_query'     => array(
        array(
            'after'  => 'December 31st, 2015', // i.e., 2016+.
            'before' => 'January 1st, 2018', // i.e., before 2018.
        ),
    ),
);
$query = new WP_Query( $args );

Based on an article written by one of the date query committers, it looks like you can use AND OR logic date queries too. So you can also do this.

$args = array(
    'posts_per_page'  => '-1',
    'date_query'      => array(
        'relation'    => 'OR',
        array( 'year' => 2016 ),
        array( 'year' => 2017 ),
    ),
);
$query = new WP_Query( $args );

All possible arguments according to that author.

'date_query' => array(
    'column' => 'optional, column to query against, default is post_date',
    'compare' => 'optional, see WP_Date_Query::get_compare()',
    'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND',
    array(
        'column' => 'see above',
        'compare' => 'see above',
        'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
        'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
        'inclusive' => 'boolean, for after/before, whether exact value should be matched or not',
        'year' => '4 digit int',
        'month' => 'int, 1-12',
        'week' => 'int, 0-53',
        'day' => 'int, 1-31',
        'hour' => 'int, 0-23',
        'minute' => 'int, 0-60',
        'second' => 'int, 0-60',
    ),
    array(
        ...
    ),
    ..
),
Source Link
jaswrks
  • 851
  • 5
  • 8

You can use before and after, see date arguments.

$args = array(
    'posts_per_page' => '-1',
    'date_query'     => array(
        array(
            'after'  => 'December 31st, 2015', // i.e., 2016+.
            'before' => 'January 1st, 2018', // i.e., before 2018.
        ),
    ),
);
$query = new WP_Query( $args );