查询post_meta日期而不是post_date_gmt

时间:2017-04-09 00:09:21

标签: php arrays wordpress date strtotime

我限制我的查询使用6个月前的show post工作正常。

但我需要它基于post_meta表中的日期而不是'post_date_gmt'。

在我的情况下,我将meta_keys称为 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
);

3 个答案:

答案 0 :(得分:3)

在此处查看:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

如果您滚动示例,则会看到:

  

显示自定义字段键为设置日期且自定义字段值为现在的帖子。仅显示未通过的日期。

Replace All

在您的情况下,您可以输入类似:for (i in 2:nrow(TestData)) { # If stock - demand <= threshold, restock if ((TestData[i-1, "Stock"] - TestData[i, "Demand"]) <= TestData[i, "Threshold"]) { TestData[i, "Stock"] <- TestData[i-1, "Upto"] } # Else update stock with stock - demand else { TestData[i, "Stock"] <- TestData[i-1, "Stock"] - TestData[i, "Demand"] } }

的内容

答案 1 :(得分: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 );

检查您的WordPress网站中的post_type是否为真。

答案 2 :(得分:2)

  

首先date_query首先对post_date_gmt起作用。如果你想   要从元字段查询帖子,您必须使用meta_query

以下是示例代码:

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

以上代码应该适合您。

相关问题:

希望这有帮助!