How To Compare Between Two Dates In A WordPress Loop

Looking to compare between two dates in a WordPress loop? All you may need is to pass these arguments into your loop’s argument array.

Looping between dates In WordPress

To loop between two dates in WordPress, you need to pass the following arguments into your WordPress query’s loop.

For example, in a scenario where you need to loop from the first day of the month to the last:

$start = date( 'Ym01' );
$end = date( 'Ym01', strtotime( '+1 month' ) );
$args = array(
     'post_type' => 'example_post_type',
     'posts_per_page' => -1,
     'meta_key' => 'date_completed',
     'meta_value' => array( $start, $end ),
     'meta_compare' => 'BETWEEN',
     'type' => 'date'
);

Note, the date must be in Ymd format to compare between the two.