Request post_meta date instead of post_date_gmt

I limit my request to use a show message from 6 months ago, which works great.

But I need it to be based on a date that is in the table post_metainstead of "post_date_gmt".

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

$months_ago = 6;
$args = array(
'date_query' => array(
    array(
        'column' => 'post_date_gmt',
        'after'  => $months_ago . ' months ago',
        )
    ),
'numberposts'   => -1
);
+6
source share
3 answers

First, the first date_queryworks on post_date_gmt. If you want to request a message from meta fields, you should 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.

:

, !

+2

: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

, :

, - , . , .

$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 );

- : date( "dmY", strtotime( '6 months ago' ) )

+3

:

$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 );

, post_type WordPress.

+1

Source: https://habr.com/ru/post/1016410/


All Articles