Query post_meta date instead of post_date_gmt
Asked Answered
O

3

6

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
);
Ontogeny answered 9/4, 2017 at 0:9 Comment(0)
S
2

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!

Schick answered 17/4, 2017 at 8:17 Comment(0)
T
3

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

Trimetallic answered 12/4, 2017 at 15:18 Comment(0)
B
3

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.

Berenice answered 17/4, 2017 at 8:26 Comment(0)
S
2

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!

Schick answered 17/4, 2017 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.