Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query post_meta date instead of post_date_gmt

I'm limiting my query to use show post from 6 months ago which works fine.

But I need it to be based on a date that is in the post_meta table instead of 'post_date_gmt'.

In my case I have meta_keys are called payment_date and the values are of course a date like 31-10-2016 for example.

$months_ago = 6;
$args = array(
'date_query' => array(
    array(
        'column' => 'post_date_gmt',
        'after'  => $months_ago . ' months ago',
        )
    ),
'numberposts'   => -1
);
like image 296
User_FTW Avatar asked Apr 09 '17 00:04

User_FTW


1 Answers

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' ) )

like image 93
Antonino Scarfì Avatar answered Sep 18 '22 00:09

Antonino Scarfì