Query post_meta date instead of post_date_gmt Query post_meta date instead of post_date_gmt wordpress wordpress

Query post_meta date instead of post_date_gmt


Check it here: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

If you scroll the examples, you'll see:

Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed.

$args = array(    'post_type'    => 'event',    'meta_key'     => 'event_date',    'meta_value'   => date( "Ymd" ), // change to how "event date" is stored    'meta_compare' => '>',);$query = new WP_Query( $args );

In your case you could put something like: date( "dmY", strtotime( '6 months ago' ) )


Try this code:

$sixmonthagodate = date( "d-m-Y", strtotime('-6 Months') );$args = array(    'post_type'    => 'post',    'posts_per_page' => -1,    'meta_key'     => 'payment_date',    'meta_value'   => $sixmonthagodate,    'meta_compare' => '>',);$query = new WP_Query( $args );

check that if your post_type is true in your WordPress site.


First thing first date_query works on post_date_gmt. If you want to query post from meta fields then you have to use meta_query.

Here is a sample code:

$months_ago = 6;$args = [    //...    //...    'posts_per_page' => -1, // Unlimited posts    //...    //...    'meta_query' => [        'relation' => 'AND',        [            'key' => 'payment_date',            'value' => date('d-m-Y', strtotime($months_ago . ' months ago')), //<-- Converting date into your custom date format            'compare' => '>', //you can also use <=, >=, <, etc..            'type' => 'DATE'        ],    ]];$query = new WP_Query($args);if ($query->have_posts()) :    /* Start the Loop */    while ($query->have_posts()) : $query->the_post();    //you post    endwhile;endif;

The above code should work for you.

Related Question:

Hope this helps!